Hi,
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>";
cs.log(cs.xml_to_json(testXML))
And this is the result I’m getting:
{"tag2":"Test"}
Has anyone had issues with this before or would be able to advise on any alternatives?
Thanks,
Ewan
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: {}
Thanks,
Ewan