Question

JSON Payload sorting

Hi,

Is there a way to order the outgoing JSON and ensure that the order remains the same every time.

We have the following JSON where we have to re-order before sending it to the WSO2 as the service architecture is designed like that.

So is it possible to do so in PEGA?(assume we set the values in the correct order expected in the service using the request datatransform)

{
"reason":"Test ",
"card":"testcard",
"user":"testapp",
"channel":"testchannel",
"cardStatusCode":00
}

Thanks

Kamalesh

Comments

Keep up to date on this post and subscribe to comments

Pega
September 30, 2019 - 7:32am

Hi,

Suggest you look at the Java in the Activity Invoke, the ServiceUtils public API interface converts the clipboard values with:

     utils.mapOutboundData(resourceParams, (Map) resourceParamVals, params, (ClipboardPage) dataPage );

 

Since plain Maps aren't normally ordered, you'll need to override Invoke and add a little Java. See step 5 and the line

 

     java.util.HashMap resourceParamVals = new java.util.HashMap();

 

My suggestion would be to change HashMap to a TreeMap and feed its constructor a Comparator you instantiated on the preceding line with an anonymous implementation that sorts your parameters in the order desired. Of course you need context-specific Comparators, so maybe a circumstanced or parameterized activity responsible for just creating the Comparator would be best. Invoke calls it to receive the Comparator before the current step 5, it gets set it to a local Object variable before step 5, and step 5's only change is:

 

     java.util.HashMap resourceParamVals = new java.util.TreeMap(comparator);

 

It should work!