add additional json element in response

Hi ,

 

Is it possible to add an additional json tag to the response from target system?

ex:

 

{

"a":"x",
"b":y",

"c":"z"

}

 

to be transformed to

 

{

"a":"x",
"b":y",

"b2":y2",

"c":"z"

}

Regards

Sujith Mathew

Solved Solved
0 2 725
1 ACCEPTED SOLUTION

Yes, I would do this with a JavaScript callout.  It looks like this:

 

var c = JSON.parse(context.getVariable('response.content'));
c.b2 = 'whatever value you like';
context.setVariable('response.content', JSON.stringify(c));

 

 You need to attach that in the Response flow.  

Of course you could also delete fields from the response.

 

var c = JSON.parse(context.getVariable('response.content'));
delete c.b ;
context.setVariable('response.content', JSON.stringify(c));

 

Or you could combine these obviously. You could add some fields, delete others. 

And in general you could re-shape the response using any logic you like implemented in JavaScript. For example, you could apply some sort of templating engine.   Or you could filter out all fields not specified in an include list, or filter out all fields specified in an Exclude list.  And those lists could be dynamic by API Product, or app, or client.  Many possibilities.

I described some other possibilities and provided a working sample of "Defaults" in this reply.

View solution in original post

2 REPLIES 2

I'm not sure if you can do this on the fly, I think you need to get backend.response object, parse it to json, and then append relevant extra data. Or use XSLT to transform... @dchiesa1 might have additional ideas. 

Yes, I would do this with a JavaScript callout.  It looks like this:

 

var c = JSON.parse(context.getVariable('response.content'));
c.b2 = 'whatever value you like';
context.setVariable('response.content', JSON.stringify(c));

 

 You need to attach that in the Response flow.  

Of course you could also delete fields from the response.

 

var c = JSON.parse(context.getVariable('response.content'));
delete c.b ;
context.setVariable('response.content', JSON.stringify(c));

 

Or you could combine these obviously. You could add some fields, delete others. 

And in general you could re-shape the response using any logic you like implemented in JavaScript. For example, you could apply some sort of templating engine.   Or you could filter out all fields not specified in an include list, or filter out all fields specified in an Exclude list.  And those lists could be dynamic by API Product, or app, or client.  Many possibilities.

I described some other possibilities and provided a working sample of "Defaults" in this reply.