Hello Sai
Well a simple JSON-to-XML policy won’t satisfy, because you want to change the structure and the names of the properties. You want to:
- change the name of orderNumber to ordNbr
- change the name of customerId to custNbr
- remove the phoneNumber field
- add two levels of parent element, dupOrderCheckRow and dupOrderCheck
- and then finally, transform the JSON to XML
You cannot do all of that in a single JSON-to-XML step. For that you need two steps. You have a couple options.
- a JSON -to-XML step, followed by an XSL step to modify the structure, OR
- a JavaScript step to modify the structure, followed by a JSON-to-XML step.
I have produced an example of the latter. The JavaScript looks like this:
var c = context.getVariable('request.content');
var json = JSON.parse(c);
var newObject = {
dupOrderCheck : {
"#namespaces": {
"xsi": "http://www.w3.org/2001/XMLSchema-instance"
},
dupOrderCheckRow : {
ordNbr : json.orderNumber,
custNbr : json.customerId
}
}
};
context.setVariable('request.content', JSON.stringify(newObject));
And the JSON-to-XML step is like this:
<JSONToXML name="JSON-to-XML-1">
<Options>
<NamespaceBlockName>#namespaces</NamespaceBlockName>
<DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
<NamespaceSeparator>:</NamespaceSeparator>
</Options>
<OutputVariable>response</OutputVariable>
<Source>request</Source>
</JSONToXML>
The results are like this:
$ curl -i [https://ORG-ENV.apigee.net/sai-jsontoxml](https://ORG-ENV.apigee.net/sai-jsontoxml) \
-H content-type:application/json \
-d '{
"orderNumber":"1111",
"customerId":"1234",
"phoneNumber":"1234-4456-2983"
}
'
HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 22:57:35 GMT
Content-Length: 190
Connection: keep-alive
Server: Apigee Router
<dupOrderCheck xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dupOrderCheckRow>
<ordNbr>1111</ordNbr>
<custNbr>1234</custNbr>
</dupOrderCheckRow>
</dupOrderCheck>
Please find a working API proxy attached here.
sai-jsontoxml-rev1-2017-09-05.zip
Also note: the xmlns:xsi namespace attribute is unused, and ought to be unnecessary. If your receiving system “needs” that, it’s broken. One might also say that a system that generates XML with unused XML namespaces is broken. (I’ve generated it because you asked for it) The XML above is equivalent to the following:
<dupOrderCheck>
<dupOrderCheckRow>
<ordNbr>1111</ordNbr>
<custNbr>1234</custNbr>
</dupOrderCheckRow>
</dupOrderCheck>