Search code examples
jsonwso2wso2-esbwso2-integration-studio

How to combine 2 json payloads in wso2?


I am making use of XSLT mediator to form the following JSON payload:

{
    "products": [
        {
          
            "product_id": 1,
            "product_name" : "abc"
           
        },
        {
            
            "product_id": 2,
           "product_name" : "xyz"
            
        }
    ]
}

Based on the response I get from the api using the above payload, I need to create another json payload(below) and append it to make another api call.

{
"amount": {
    "total_amount": 0.46,
    "total_tax": 0
  }
 }

I want the final payload to look like

 {
    "products": [
        {
          
            "product_id": 1,
            "product_name" : "abc"
           
        },
        {
            
            "product_id": 2,
           "product_name" : "xyz"
            
        }
    ],
    
    "amount": {
    "total_amount": 0.46,
    "total_tax": 0
  }
}

How can I achieve this on WSO2 Integration studio?


Solution

  • I think XSLT is an overkill for this and I don't think Datamapper is a viable option here, given we are dealing with multiple inputs. You can simply use the Enrich Mediator for this. Here is an example for your payloads. I tried to provide an example as close to your requirement, I have hardcoded the payloads, and the Payload Factory depicts your backend call response and the original PL.

    <?xml version="1.0" encoding="UTF-8"?>
    <api context="/json" name="TestAPI" xmlns="http://ws.apache.org/ns/synapse">
        <resource methods="GET">
            <inSequence>
                <payloadFactory description="Build JSON" media-type="json">
                    <format>
                        {
                            "products": [
                                {
                                    "product_id": 1,
                                    "product_name" : "abc"   
                                },
                                {  
                                   "product_id": 2,
                                   "product_name" : "xyz"                               
                                }
                            ]
                        }
                        </format>
                    <args/>
                </payloadFactory>
                <enrich description="First save the Original PL to a property" >
                    <source clone="true" type="body"/>
                    <target property="ORIGINAL_PL" type="property"/>
                </enrich>
                <payloadFactory description="Build JSON second PL" media-type="json">
                    <format>
                        {
                            "amount": {
                                "total_amount": 0.46,
                                "total_tax": 0
                              }
                        }
                        </format>
                    <args/>
                </payloadFactory>
                <enrich description="Save the PL from the second request to another property">
                    <source clone="true" type="body"/>
                    <target property="SECOND_PL" type="property"/>
                </enrich>
                <enrich description="Restore original payload">
                    <source clone="false" property="ORIGINAL_PL" type="property"/>
                    <target type="body"/>
                </enrich>
                <enrich description="Now add the second PL as a Child to the original" >
                    <source clone="true" property="SECOND_PL" type="property"/>
                    <target action="child" xpath="json-eval($)"/>
                </enrich>
                <log level="full"/>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
    </api>
    

    This is the result of the above.

    {
      "products": [
        {
          "product_id": 1,
          "product_name": "abc"
        },
        {
          "product_id": 2,
          "product_name": "xyz"
        }
      ],
      "amount": {
        "total_amount": 0.46,
        "total_tax": 0
      }
    }