Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert XML string to JSON format

0 Kudos

Hi,

I have a requirement where I have to convert a string in XML format into corresponding JSON format.

The existing serializer does not recognise the tags and treats the entire string as one long text.

I tried using the XML parser( SMUM_XML_PARSE) which would return an internal table with the tags and corresponding values. JSON string generated from it treats the tags and values as value fields.

I would like to know whether my approach is right here or whether I should go about it differently.  Also, I would appreciate some guidance on converting a string in XML format into corresponding JSON format.

Regards,

Renjith

3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos

One XML may correspond to several possibilities in JSON. I guess there could be a minimalist conversion function module (or whatever), but I feel it would be no sense, so my conclusion is that you always need a home made algorithm to convert. Why not with SMUM_XML_PARSE if it suits your requirement.

EDIT:

For instance, who knows if <tab><line>1<line></tab> is to be converted into {"tab":{"line":1}} or into {"tab":[{"line":"1"}]}

So you must create a program (see Horst answer)

PS: maybe a tool could be developped, which receives both XML + XSD, then it would be able to convert correctly.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

If you want to do it in ABAP, you might

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

I've written an example, how one could do it in ABAP now, see