Powered By Blogger

Aug 29, 2016


Using a Script Mediator to process JSON payload


This blog post describes about using a script mediator to process  JSON payload

usecase:

When the JSON payload has spaces and special characters for element names, then replace those spaces and characters as required

sample JSON:

{
  "result": [
    {
      "id": [
        "22174"
      ],
      "description": [
        "test 123"
      ],
      "Application Type": [
        "Sample Application"
      ],
      "{Sample} ID": [
        "ID1"
      ]
    }
  ]
}


Please follow below steps:

1. In the proxy service configuration, first you need to assign the JSON payload to a property as below, (this will assign the array inside the 'result')

       <property name="JSONPayload" expression="json-eval($.result[0])" />
      
2. Then define the script mediator as below

       <script language="js" key="conf:/repository/transform.js" function="transform" />
      
based on this you need to create a registry resource (.js) in the registry ( eg: at conf:/repository/) and need to add the js script into that file

3. In this sample we are going to remove the spaces and curly braces from the elements, hence we have used below script.

function transform(mc) {
    var obj = mc.getProperty('JSONPayload'); //gets the property from the message context
    var jsonObject = JSON.parse(obj); // casting the json string to a json object

    var jsonArray = {
        result: []     // initializing the array
    };
    var item = {}
    for (var prop in jsonObject) {
        var attrName = prop;
        var att = attrName.replace(/ /g, '');  //replacing white spaces
        var att = att.replace(/[{}]/g, ""); //replacing curly braces
        var attrValue = jsonObject[prop];
        var jsonText = JSON.stringify(att);
        item[att] = attrValue;
    }
    jsonArray.result.push(item); //adds the sanitized json to the defined array
    mc.setPayloadJSON(jsonArray); //set the created json payload to the message context
 }



Please find the below complete proxy configuration as a reference

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="sampleProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <call>
            <endpoint key="conf:repository/sampleEP" />
         </call>

         <property name="JSONPayload" expression="json-eval($.result[0])" />
         <script language="js" key="conf:/repository/transform.js" function="transform" />
           <log level="full">
            <property name="************RESULT************" expression="$body" />
         </log>
      </inSequence>
   </target>
   <description />
</proxy>


Final result will be as below:


<jsonObject xmlns="http://ws.apache.org/ns/synapse">
   <result>
      <id>22174</id>
      <description>test 123</description>
      <ApplicationType>Sample Application</ApplicationType>
      <SampleID>ID1</SampleID>
   </result>
</jsonObject>



Script Mediator - https://docs.wso2.com/display/ESB481/Script+Mediator
JSON support in ESB - https://docs.wso2.com/display/ESB481/JSON+Support

No comments:

Post a Comment