Layer7 API Management

JSON Conversion Assertion : Using XSLT to do XML->JSON Conversion 

Feb 28, 2019 06:06 PM

Alternative #1 to JSON Conversion Assertion : Using XSLT to do XML->JSON Conversion. 

(moved this document to it's own article from : XML-JSON Converter since could not add attachments to comments there.  The Layer 7 Gateway documentation says that XSLT and Javascript can be used when the vanilla JSON converter assertion is not configurable enough.  This article is follow up with the basics for using XSLT to do the conversion)

 

Related articles :

JSON Conversion Assertion : Using Javascript to do XML->JSON Conversion 

XML-JSON Converter 

 

Background:

 

In two support cases I was involved in, we had the issue, where the customer required the order of the elements after the JSON Transformation to be in the same order as they were in the XML file.  The default JSON Tranformation Assertion was changing the order of the elements.  Interestingly enough, if we reverse the elements they still do not appear in the same order, it seems the JSON Transformation assertion processes those elements with children before processing elements that do not have children. 

 

As mentioned in the Gateway documentation,  Apply JSON Transformation Assertion - CA API Gateway - 9.4 - CA Technologies Documentation   :

Tips: (1) If you need to transform XML to JSON, consider the Apply XSL Transformation Assertion instead for greater control. In most cases, a JSON to XML transformation is normally followed by an the Apply XSL Transformation Assertion to produce more sophisticated results.

 

1. Getting XSLT Transformation

So, we went to look for sample XSLT to do that transformation.  

https://stackoverflow.com/questions/24122921/xsl-to-convert-xm

l-to-json 

I will add it in as attachment

 

 

2. Create Policy for Apply XSL Transform  Assertion 

 

We just created simple policy, where we set the rawXML as the whole soap post data, extract out the element we want to convert to JSON.  Then apply the XSL Transformation and return the output. 

(the policy will also be attached) 

 

3. Input Data 

 

Here is the format (and order) of our test input data. 

 

 

4. Run Test Case

 

And here is the results of running the tranform policy: 

 

...

 

 

I will attach the policy and the XSLT file.

 

Cheers - Mark

Statistics
0 Favorited
7 Views
3 Files
0 Shares
5 Downloads
Attachment(s)
zip file
testXMLtoJSONViaXSLT.xml.zip   3 KB   1 version
Uploaded - May 29, 2019
zip file
test-case.xml.zip   828 B   1 version
Uploaded - May 29, 2019
zip file
xslt-convert-xml-to-json.xslt.zip   716 B   1 version
Uploaded - May 29, 2019

Related Entries and Links

No Related Resource entered.