Add response parameter in JSON response

Hi,

I have a JSON response after changing soap response from back end to JSON.

{ "Status": "PASSED", "ErrorCode": 1000, "MessageText": "Request processed successfully" }

I have to add {"TransactionID":"GFF567"} in this response. What is the best possible way to add it. I tried using Assign Message Policy but that doesn't seems to work.

Solved Solved
0 2 1,692
1 ACCEPTED SOLUTION

After soap to json, add a javascript policy, as code:

var content = context.getVariable(“response.content”);
var data = JSON.parse(content);
data.TransactionID = “GFF567”;
context.setVariable(“response.content”, JSON.stringify(data));

View solution in original post

2 REPLIES 2

After soap to json, add a javascript policy, as code:

var content = context.getVariable(“response.content”);
var data = JSON.parse(content);
data.TransactionID = “GFF567”;
context.setVariable(“response.content”, JSON.stringify(data));

Another option would be to run an XSLT after getting the SOAP response and add the TransactionID element in the XML. Then it would automatically appear in the JSON after the XML2JSON transform.

Probably not very efficient, but it would work fine.

Assuming you want to add TransactionID in the root element, this XSL should do:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<!-- match the root element of unknown name -->
<xsl:template match="/*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <!-- add a new element at the end -->
        <TransactionID>GFF567</TransactionID>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

(XSLT based on example found on https://stackoverflow.com/questions/25957167/xslt-to-add-new-elements-to-the-root-element-of-an-xml?...