Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejavascript
{"array":[1,2]}

XML (RJsonStreamBuilder):

Code Block
languagehtml/xml
<jsonObject>
   <array>1</array>
   <array>2</array>
</jsonObject>

XML (JsonBuilder):

Code Block
languagehtml/xml
<jsonObject>
   <?xml-multiple array?>
   <array>1</array>
   <array>2</array>
</jsonObject>

...

Code Block
languagejavascript
{"array":[]}

XML (RJsonStreamBuilder):

Code Block
languagehtml/xml
<jsonObject></jsonObject>

XML (JsonBuilder):

Code Block
languagehtml/xml
<jsonObject>
   <?xml-multiple array?>
</jsonObject>

...

Code Block
languagejavascript
[1,2]

XML (RJsonStreamBuilder):

Code Block
languagehtml/xml
<jsonArray>
   <jsonElement>1</jsonElement>
   <jsonElement>2</jsonElement>
</jsonArray>

XML (JsonBuilder):

Code Block
languagehtml/xml
<jsonArray>
   <?xml-multiple jsonElement?>
   <jsonElement>1</jsonElement>
   <jsonElement>2</jsonElement>
</jsonArray>

...

Code Block
languagejavascript
[1, []]

XML (RJsonStreamBuilder):

Code Block
languagehtml/xml
<jsonArray>
   <jsonElement>1</jsonElement>
   <jsonElement>
       <jsonArray></jsonArray>
   </jsonElement>
</jsonArray>

XML (JsonBuilder):

Code Block
languagehtml/xml
<jsonArray>
   <?xml-multiple jsonElement?>
   <jsonElement>1</jsonElement>
   <jsonElement>
       <jsonArray>
           <?xml-multiple jsonElement?>
       </jsonArray>
   </jsonElement>
</jsonArray>

...

Code Block
languagehtml/xml
linenumberstrue
<proxy xmlns="http://ws.apache.org/ns/synapse"
     name="singleresponse"
     transports="https,http"
     statistics="disable"
     trace="disable"
     startOnLoad="true">
     <target>
         <outSequence>
             <payloadFactory media-type="json">
                 <format>{
                             "location_response" : {
                                 "name" : "$1",
                                 "tags" : $2
                         }}
                 </format>
                 <args>
                     <arg evaluator="json" expression="$.name"/>
                     <arg evaluator="json" expression="$.types"/>
                 </args>
             </payloadFactory>
             <send/>
         </outSequence>
         <endpoint>
             <address uri="http://localhost:828018280/location"/>
         </endpoint>
     </target>
 <description/>
</proxy>

...

Code Block
languagehtml/xml
linenumberstrue
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="locations"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <outSequence>
         <script language="js"
                 key="conf:/repository/esb/transform.js"
                 function="transform"/>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:828018280/locations"/>
      </endpoint>
   </target>
   <description/>
</proxy>

The registry resource transform.js contains the JavaScript function that perfoms performs the transformation:

Code Block
languagejavascript
linenumberstrue
function transform(mc) {
    payload = mc.getPayloadJSON();
    results = payload.results;
    var response = new Array();
    for (i = 0; i < results.length; ++i) {
        location_object = results[i];
        l = new Object();
        l.name = location_object.name;
        l.tags = location_object.types;
        l.id = "ID:" + (location_object.id);
        response[i] = l;
    }
    mc.setPayloadJSON(response);
}

...

Code Block
languagehtml/xml
<locations>
   <location>
      <id>7eaf7</id>
      <name>Biaggio Cafe</name>
      <tags>bar,restaurant,food,establishment</tags>
   </location>
   <location>
      <id>3ef98</id>
      <name>Doltone House</name>
      <tags>food,establishment</tags>
   </location>
</locations>

Finally, the next section describes how to let's look at how you can perform delete, modify, and add new fields field operations on JSON payloads with the Script Mediator mediator in JavaScript. If we Let's send the JSON message returned by the "theĀ locations" proxy service as the request for the below following proxy service, transformjson:

Code Block
languagehtml/xml
linenumberstrue
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="transformjson"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <script language="js"><![CDATA[
           payload = mc.getPayloadJSON();
           for (i = 0; i &lt; payload.length; ++i) {
               payload[i].id_str = payload[i].id;
               delete payload[i].id;
               payload[i].tags[payload[i].tags.length] = "pub";
           }
           mc.setPayloadJSON(payload);
         ]]></script>
         <log>
            <property name="JSON-Payload" expression="json-eval($.)"/>
         </log>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>

...

Note that the transformation (line 9 through 17) has added a new field 'id_str' and removed the old field 'id' from the request, and it has added a new tag 'pub' to the existing tags list of the payload.

Following samples For additional examples that demonstrate different ways to manipulate JSON payloads within the ESB mediation flow., see the following samples:

Anchor
troubleshooting
troubleshooting

...