Home > Mobile >  How to combine 2 json payloads in wso2?
How to combine 2 json payloads in wso2?

Time:09-17

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?

CodePudding user response:

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
  }
}

CodePudding user response:

You can use DataMapper mediator to provide input and output schema and transform the incoming payload to the way you need. Provide either JSON or XML schema that you are receiving from the 1st service as the input schema to DataMapper and provide the schema of the expected result in either JSON or XML to the DataMatter as output schema. After that, you can map both input/ output fields and at runtime, DataMapper will do the message translation for you.

For more details - https://apim.docs.wso2.com/en/latest/tutorials/integration-tutorials/transforming-message-content/

Thanks!

  • Related