rami89
rami89

Reputation: 41

Concatenate two JSON responses in wso2 esb

I have two json from two different endpoints, i need to concatenate to convert it into a single valid json. I did it, but the first WSO2 json returns it as a string (firstjson). I would like it to be all a json.

FIRST JSON

{
    "first": true,
    "second": {
        "name": "manoj",
        "age": "45"
    },
    "third": {
        "fourth": [
            {
                "class": "test12",
                "salary": "123456"
            },
            {
                "class": "test23",
                "salary": "15678"
            }
        ],
        "fifth": "hello"
    }
}

SECOND JSON

[ { "item1": "123456", "item2": "5678" }, { "item1": "8976", "item2": "abcd" } ]

XML API

<api context="/concat" name="concat" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET" url-mapping="/concatenare">
        <inSequence>
            <call>
                <endpoint>
                    <http method="get" uri-template="http://www.mocky.io/v2/56b2d88c13000057518945d4">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>1</progressionFactor>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </http>
                </endpoint>
            </call>
            <enrich>
                <source clone="false" type="body"/>
                <target property="first-json" type="property"/>
            </enrich>
            <log level="custom">
                <property expression="get-property('first-json')" name="First json"/>
            </log>
            <call>
                <endpoint>
                    <http method="get" uri-template="http://www.mocky.io/v2/56b2d87d1300007c518945d3">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>1</progressionFactor>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </http>
                </endpoint>
            </call>
            <log level="custom">
                <property expression="get-property('first-json')" name="*********BEFOREEEEEEEE"/>
            </log>
            <!-- <enrich>
                <source clone="false" property="first-json" type="property"/>
                <target type="body"/>
            </enrich>  -->
            <payloadFactory media-type="xml">
                <format>
                    <completeJson xmlns="">
                        <firstjson>$1</firstjson>
                        <secondjson>$2</secondjson>
                    </completeJson>
                </format>
                <args>
                    <arg evaluator="xml" expression="get-property('first-json')"/>
                    <arg evaluator="xml" expression="$body"/>
                </args>
            </payloadFactory>
            <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

RESPONSE API

{
    "completeJson": {
        "firstjson": "{\"first\":true,\"second\":{\"name\":\"manoj\",\"age\":\"45\"},\"third\":{\"fourth\":[{\"class\":\"test12\",\"salary\":\"123456\"},{\"class\":\"test23\",\"salary\":\"15678\"}],\"fifth\":\"hello\"}}",
        "secondjson": {
            "Body": [
                {
                    "item1": 123456,
                    "item2": 5678
                },
                {
                    "item1": 8976,
                    "item2": "abcd"
                }
            ]
        }
    }
}

Where am I wrong ?

Thanks in advance guys

Upvotes: 1

Views: 209

Answers (1)

rami89
rami89

Reputation: 41

Ok guys,

I solved it, I was wrong with the payloadFactory, the format it had to be of type json.

   <payloadFactory media-type="json">
    <format>
        {
            "completeJson" : {
            "firstjson" : $1,
            "secondjson" : $2
        }}
    </format>
        <args>
            <arg expression="get-property('first-json')" />
            <arg expression="$body" />
        </args>
</payloadFactory>

thanks anyway guys :)

Upvotes: 3

Related Questions