XML to JSON function not working as expected


I’m trying to parse an XML string to JSON in the code studio and I’ve found the cs.xml_to_json() function, however I’m having an issue with getting this to output properly, when there are nested tags it doesn’t seem to nest them in the json.

This is the code im using:

const testXML = "<tag1><tag2>Test</tag2></tag1>";

And this is the result I’m getting:


Has anyone had issues with this before or would be able to advise on any alternatives?


I believe the outer most tags are considered to be the document itself, and only its contents are returned. You can wrap your content in an arbitrary extra tag pair if you want to return the root tags from your original xml, e.g:

cs.log(cs.xml_to_json('<xml>' + testXML + '</xml>'));

Hi Bob,

We’ve found what the problem was, it didn’t like the xmlns: in our opening tag (see below)

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

Does this seem like a bug, we were just getting an empty object like this in return: {}