Layer7 API Management

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

Mar 05, 2019 06:48 PM

Alternative #2 to JSON Conversion Assertion : Using Javascript to do XML->JSON Conversion. 

(this document is follow up from : XML-JSON Converter 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 Javascript to do the conversion)

 

Note:  I ended up getting caught on this one, for too much time.  From some early testing I "thought" that our javascript assertion was not accepting recursive calls.  Since you can usually replace recursive calls using a loop and a stack, I worked on that, and got it working, only to find after I had it working that function declaration and recursive calls do work in our javascript assertion.  Overall that's a good thing since it gives a much simpler solution, but does means I spent/wasted a lot of time on this now fairly simple article. 

 

Related articles :

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

XML-JSON Converter 

 

 

1. Javascript to convert XML to JSON 

There are a bunch of methods via google, here is the one I used.   https://davidwalsh.name/convert-xml-json  to include in my javascript assertion. 

 

It needed some tweaks to use the available XML navigation methods.  Execute JavaScript Assertion - CA API Gateway - 9.4 - CA Technologies Documentation 

And I did not (in my case) have any attributes, so I have not transferred that from David's method.  

 

So in the end the javascript assertion is fairly simple : (see attachment at end) : 

 

 

2. Create Policy for JavaScript 

The body for the policy is the same as the one used for the XSLT example. (see attachment at end) - I've left my non-recursive solution in there as well - after spending the time on it didn't want to delete it just yet. 

 

 

3. Input Data 

 

Here is the format (and order) of our test input data - as per the XSLT example. : 

 

 

4. Run Test Case

 

And here is the results of running the transform policy: 

      http://odoma04-gw94.ca.com:8080/testXMLtoJSONViaJavaScript 

Giving the json formatted output : 

 

 

 

Attached are the : 

  testXMLtoJSONViaJavascript.xml  : Gateway policy for the test harness and javascript method.

assertion-conv-to-json.js  : The javascript used in the assertion 

test-case.xml : The input xml test case used. 

 

Cheers - Mark

Statistics
0 Favorited
10 Views
3 Files
0 Shares
9 Downloads
Attachment(s)
zip file
testXMLtoJSONViaJavascript.xml.zip   5 KB   1 version
Uploaded - May 29, 2019
zip file
assertion-conv-to-json.js.zip   571 B   1 version
Uploaded - May 29, 2019
zip file
test-case.xml.zip   828 B   1 version
Uploaded - May 29, 2019

Tags and Keywords

Comments

Sep 12, 2019 01:06 AM

the embedded screenshots are still missing in this post - since conversion from the original communities transfer - Cheers - Mark

Related Entries and Links

No Related Resource entered.