XML TO JSON

Not applicable

Hi Friends,

I am working on converting XML into JSON. Below is structure of XML and expect JSON

XML Data :

<root>

<personalDetails>

<name>name1</name>

<age>21</age>

</personalDetails>

<personalDetails>

<name>name2</name>

<age>24</age>

</personalDetails>

</root>

Expected output :

{"personalDet":[["name1","21"],["name2","24"]}

Actual output :

{"personalDet":["name1","21", "name2","24"]}

XSD is used is like

root --> personalDetails*

Please help me how I can get JSON object as mentioned above

Solved Solved
0 3 1,846
1 ACCEPTED SOLUTION

Not applicable

Vinod,

My colleague @Mike Dunker has a good solution to this problem. It involves augmenting the XML payload with some "dummie" elements so that the XMLtoJSON policy formats the data correctly. In a subsequent step you remove the hints from your payload. This is often easier than doing the translation your self.

Take a look at: http://stackoverflow.com/questions/22008527/xml-to-json-conversion-issues

For a detailed explanation.

-David

View solution in original post

3 REPLIES 3

Hi Vinod,

You can transform a request or response object from XML to JSON using the XMLtoJSON policy on Apigee Edge. Here's a link to the doc topic on that policy: http://apigee.com/docs/api-services/reference/xml-json-policy

You can find a working sample in the api-platform-samples repo on GitHub, here: https://github.com/apigee/api-platform-samples/tree/master/sample-proxies/xmltojson

And, you can find links to other samples in the GitHub repo that employ XMLtoJSON (or JSONtoXML) policies here: http://apigee.com/docs/api-services/content/samples-reference

Hope this helps. If you have another use case in mind, please post more details.

Not applicable

@wwitman

Hi Witman,

Thanks for your response !!

Use case is like I have declared a repeating element say (basicDetails) under root element. Now, I want to extract user details in same element as multiple rows

{ "basicDetails" : [ ["name","age"] , ["name","age"] ] }

problem occurring now is I am not able to specify columns per row. So, end result what I am getting is -

["name" , "age" , "name" , "age" ]

Please let me know If I am to make you understand my query

Thanks,

Vinod

Not applicable

Vinod,

My colleague @Mike Dunker has a good solution to this problem. It involves augmenting the XML payload with some "dummie" elements so that the XMLtoJSON policy formats the data correctly. In a subsequent step you remove the hints from your payload. This is often easier than doing the translation your self.

Take a look at: http://stackoverflow.com/questions/22008527/xml-to-json-conversion-issues

For a detailed explanation.

-David