How to change json objects / elements out of apigee

Not applicable

I'm currently doing some migration work of some old apps that have elements already defined. However the new content management system is outputting json with different element names. I'm hoping that I can change those names inside of Apigee.

For example, the CMS outputs:

{"shows":

   [{"Title":"Star Wars","NodeID":"77"}]}

And the App looks for:

{"shows":

   [{"Title":"Star Wars","ShowID":"77"}]}

Is there a way to have Apigee change NodeID to ShowID?

The Apps are hard coded and can't be changed and we don't want redundant data in the CMS. I was looking at key mapping, but seeing if there might be a different approach.

Solved Solved
1 5 5,706
1 ACCEPTED SOLUTION

Not applicable

This can definitely be done. First, you would use an Extract Variables Policy to get the NodeID and Title and then use an Assign Message Policy to change the body of the payload to be what you want it to be.

In your case, you may need to use a JavaScript callout to iterate through the payload array that is found through Extract Variables. The JavaScript callout would define a new flow variable whose value would be the transformed/changed array. The Assign Message policy would change the body of request to be the value of the JavaScript flow variable.

View solution in original post

5 REPLIES 5

Not applicable

This can definitely be done. First, you would use an Extract Variables Policy to get the NodeID and Title and then use an Assign Message Policy to change the body of the payload to be what you want it to be.

In your case, you may need to use a JavaScript callout to iterate through the payload array that is found through Extract Variables. The JavaScript callout would define a new flow variable whose value would be the transformed/changed array. The Assign Message policy would change the body of request to be the value of the JavaScript flow variable.

Not applicable

Thanks, I'll give it a try.

Not applicable

SO in theory I have this working, with one exception. I have a JavaScript that will transform the array, but I can only seem to get it to work against mockup data that is outlined in a Message policy. I can't seem to be able to get the JS to read the JSON "response" coming into the proxy. Either I'm doing something wrong on the Extract Variables or there's something I'm missing with the JS reading the response.

Any additional thoughts?

Not applicable

You should be able to see the value of any flow variable you are creating in the Trace tool. That would help you see if there is a problem with your Extract Variables policy or if the problem is elsewhere.

Here is an example Extract Variables policy if it helps at all:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Comments">
    <DisplayName>Extract Comments</DisplayName>
    <FaultRules/>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="comment1">
            <JSONPath>$.entities[0].comment</JSONPath>
        </Variable>
      	<Variable name="comment2">
            <JSONPath>$.entities[1].comment</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">commentResponse</Source>
</ExtractVariables>


Not applicable

I figured it out. Ultimately I was trying to go against "response" but needed to use "response.content" and it worked. Thanks!