Modify Target response properties before

Hi,

Is it possible to modify target response properties/fieldnames in response flow before sending back to consumer.

Lets say the response from target is the json below.

 

 

{

"name" : "Alex"

"address" : "street A, New York"

}

 

 

 

I would like to transform it as the following. 

 

{
"employee_name" : "Alex",
"emplyee_address" : "street A, New York"
}

 

 Thanks

0 3 85
3 REPLIES 3

Also Please note response would be a list of objects. Each object having properties name and address

There's multiple ways of doing this, you could use an ExtractVariables policy to extract data from the response, and use an AssignMessage policy to then create a new response using the extracted values.

If you're looking to add a prefix to particular fields, it might be easier to implement this in javascript. Also, you mention in your follow up it's actually a list of objects, again javascript would probably suit this better.

If you want to modify the payload that contains a JSON list, I would use JavaScript.  Introduce a JS policy into your proxy, attach it into the Target Response flow, or the PRoxy Response flow (whichever is more appropriate for you), and  you can directly manipulate the response content. It. might look like this: 

var c = JSON.parse(context.getVariable('response.content'));
if (c.items && c.items.length) {
  c.items.forEach( function(item) {
    // modify each item
    item.employee_name = item.name;
    delete item.name;
    item.employee_address = item.address;
    delete item.address;
  });
}

// add a new toplevel property if you like
c.note = "manipulated by JS callout";

// write back the modifications
context.setVariable('response.content', JSON.stringify(c, null, 2));