NxN
NxN

Reputation: 21

Get json message from soap response in wso2

...
<property expression="//soap:Envelope/soap:Body/Response/Result" name="result"/>
<log>
  <property expression="get-property('result')" name="result"/>
</log>
...

the response

...
<response>  [  { "_id": "62908b4267c5284f7fa8320e",  "index": 0, "serial": "11006883-fb2b-49ba-a6f7-0ec0daafa1c5", "Active": false, "balance": "$3,723.68", ,"age": 34 }]    </response>

How do i get the response property for id and balance? When i use

 <log>
    <property expression="get-property('response')" name="response"/>
 </log>

it only returns the Json array only only

Upvotes: 1

Views: 499

Answers (2)

Justin
Justin

Reputation: 967

We can assume payload look like below.

    <response>
[
    {
        "_id": "62908b4267c5284f7fa8320e",
        "index": 0,
        "serial": "11006883-fb2b-49ba-a6f7-0ec0daafa1c5",
        "Active": false,
        "balance": "$3,723.68",
        "age": 34
    }
]
</response>

We're going to store <response> tag values into property like below.

<property name="jsonMessageContent" expression="//response/text()"/>

Post that, going to use enrich mediator to convert property value into body.

<enrich>
        <source clone="false" xpath="get-property('jsonMessageContent')"/>
        <target type="body"/>
     </enrich>

Finally we can use xpath to fetch values like _id,balance from payload

<log level="custom">
        <property name="id value***" expression="//jsonArray/jsonElement/_id/text()"/>
        <property name="balance value***" expression="//jsonArray/jsonElement/balance/text()"/>
     </log>

Output:

id value*** = 62908b4267c5284f7fa8320e, balance value*** = $3,723.68

Upvotes: 2

ycr
ycr

Reputation: 14574

Assuming you have a valid Json within your XML response. You can do the following to get the ID and balance. Here you have to use a script mediator.

<property expression="//response/text()" name="jsonMessageContent"/>
<script language="nashornJs"><![CDATA[
// Get the Json message portion from the response. You can use property mediator extract only that part      
mc.getProperty('jsonMessageContent')
// Convert into a Json Object
var jsonObj = JSON.parse(mc.getProperty('jsonMessageContent'));
// Specify your jsonpath here and extract
var balance = jsonObj.balance;

// Set to a different property  so you can use it in your integrations
mc.setProperty("balance", balance);
]]></script>

Upvotes: 0

Related Questions