Anand
Anand

Reputation: 41

Need Help in Merging two Json Results into single response Using Mule

There are two http calls(calling rest api) which gives two different responses. first one is giving json response for Shops like below

    {
    "shops": [
        {
            "shop": {
                "code": "a",
                "streetName": "a",
                "city": "a",
                "district": "a",
                "state": "a",
                "postalCode": "a",
                "country": "a"

            }
        },
        {
            "shop": {
                "code": "b",
                "streetName": "b",
                "city": "b",
                "district": "b",
                "state": "b",
                "postalCode": "b",
                "country": "b"
            }
        }
    ]
}

other call is giving sales office for given code(code is same as for shop).it returns a single sales office for given code

    {
    "salesOffice": {
        "shop": {
            "code": "a"
        },
        "office": "a",
        "branch": "a",
        "district": "a",
        "subRegion": "a",
        "region": "a"
    }
}

my requirement is to get shops which is an json array and add sales office for each shop and return a single json response like below

 {
    "shops": [
        {
            "shop": {
                "code": "a",
                "streetName": "a",
                "city": "a",
                "district": "a",
                "state": "a",
                "postalCode": "a",
                "country": "a",
                "salesOffice": {
                    "office": "a",
                    "branch": "a",
                    "district": "a",
                    "subRegion": "a",
                    "region": "a"
                }
            }
        },
        {
            "shop": {
                "code": "b",
                "streetName": "b",
                "city": "b",
                "district": "b",
                "state": "b",
                "postalCode": "b",
                "country": "b",
                "salesOffice": {
                    "office": "b",
                    "branch": "b",
                    "district": "b",
                    "subRegion": "",
                    "region": "b"
                }
            }
        }
    ]
}

is there any way to achieve the result?

Edit

I need to get sales office for both shops( there will be a http request for each shop code) and merge it into the response. so if I get two shops then I need to send two http request to get salesoffice for those code and then merge it with the response.

so the first call will give me shops (lets say two shops). then I need to make two http calls by getting "code" present in each shop to get sales office , and then I need to merge both shops with respective sales office.

Upvotes: 0

Views: 3953

Answers (2)

Arden Vallente
Arden Vallente

Reputation: 216

You can do something like this.

enter image description here

XML file:

<flow name="merge-jsonFlow1">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/json-merge" doc:name="HTTP"/>
        <expression-component doc:name="Mock HTTP Response"><![CDATA[payload = '{"shops":[{"shop":{"code":"a","streetName":"a","city":"a","district":"a","state":"a","postalCode":"a","country":"a"}},{"shop":{"code":"b","streetName":"b","city":"b","district":"b","state":"b","postalCode":"b","country":"b"}}]}']]></expression-component>
        <set-variable variableName="shops" value="#[payload]" doc:name="Store Orig Payload to Variable"/>
        <processor-chain doc:name="Processor Chain">
            <splitter expression="#[json:/shops]" doc:name="Splitter"/>
            <set-variable variableName="storeCode" value="#[json:/shop/code]" doc:name="Set Store Code"/>
            <expression-component doc:name="Mock HTTP Call to Get SalesOffice"><![CDATA[if (flowVars['storeCode'] == 'a') {
    payload = '{"salesOffice":{"shop":{"code":"a"},"office":"a","branch":"a","district":"a","subRegion":"a","region":"a"}}';
} else if (flowVars['storeCode'] == 'b') {
    payload = '{"salesOffice":{"shop":{"code":"b"},"office":"b","branch":"b","district":"b","subRegion":"b","region":"b"}}';
}]]></expression-component>
            <collection-aggregator failOnTimeout="true" doc:name="Collection Aggregator"/>
        </processor-chain>
        <set-payload value="#[[flowVars['shops'], payload]]" doc:name="Merge Two JSON"/>
        <json:json-to-object-transformer doc:name="JSON to Object"/>
        <dw:transform-message metadata:id="c72e3e02-8350-42ec-a3cb-ca61c7b722b4" doc:name="Transform Message">
            <dw:input-payload doc:sample="json-merge.json"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
using (
    shops = payload[0].shops,
    so = payload[1].*salesOffice
)
{
    shops : shops.shop map {
        shop: using (mycode = $.code) {
                code: $.code,
                streetName: $.streetName,
                city: $.city,
                district: $.district,
                state: $.state,
                postalCode: $.postalCode,
                country: $.country,
                salesOffice: ((so map {
                    shopCode: $.shop.code,
                    office: $.office,
                    branch: $.branch,
                    district: $.district,
                    subRegion: $.subRegion,
                    region: $.region
                }) filter $.shopCode == mycode)[0]
        }
    }
}]]></dw:set-payload>
        </dw:transform-message>
    </flow>

Upvotes: 2

Arden Vallente
Arden Vallente

Reputation: 216

You can do something like this. I used dataweave to transform json.

enter image description here

The Mock API Response is http call to your api.

XML file:

    <flow name="merge-jsonFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/merge" doc:name="HTTP"/>
        <scatter-gather doc:name="Scatter-Gather">
            <expression-component doc:name="Mock API1 Response"><![CDATA[payload = '{"shops":[{"shop":{"code":"a","streetName":"a","city":"a","district":"a","state":"a","postalCode":"a","country":"a"}},{"shop":{"code":"b","streetName":"b","city":"b","district":"b","state":"b","postalCode":"b","country":"b"}}]}']]></expression-component>
            <expression-component doc:name="Mock API2 Response"><![CDATA[payload = '{"salesOffice":{"shop":{"code":"a"},"office":"a","branch":"a","district":"a","subRegion":"a","region":"a"}}']]></expression-component>
        </scatter-gather>
        <json:json-to-object-transformer doc:name="JSON to Object"/>
        <dw:transform-message metadata:id="0066a92a-85d8-4514-8459-b70252cb2f7b" doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
using (
    shops = payload[0].shops,
    salesOffice = payload[1].salesOffice
)
{
    shops : shops.shop  map {
        shop: {
                code: $.code,
                streetName: $.streetName,
                city: $.city,
                district: $.district,
                state: $.state,
                postalCode: $.postalCode,
                country: $.country,
                (salesOffice: {
                    office: salesOffice.office,
                    branch: salesOffice.branch,
                    district: salesOffice.district,
                    subRegion: salesOffice.subRegion,
                    region: salesOffice.region
                }) when $.code == salesOffice.shop.code 
        }
    }
}]]></dw:set-payload>
        </dw:transform-message>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>

Sample Response:

{
  "shops": [
    {
      "shop": {
        "code": "a",
        "streetName": "a",
        "city": "a",
        "district": "a",
        "state": "a",
        "postalCode": "a",
        "country": "a",
        "salesOffice": {
          "office": "a",
          "branch": "a",
          "district": "a",
          "subRegion": "a",
          "region": "a"
        }
      }
    },
    {
      "shop": {
        "code": "b",
        "streetName": "b",
        "city": "b",
        "district": "b",
        "state": "b",
        "postalCode": "b",
        "country": "b"
      }
    }
  ]
}

Hope this helps

Upvotes: 0

Related Questions