mitchelangelo
mitchelangelo

Reputation: 899

nodejs: xml2js returns broken data structure because it inserted escape character

xml2js adds escape character which invalidates it's own json, my xml already contains escape characters. Is there anyway to tell xml2js to not insert escape characters?

I know this because I took a look in the response and found this '5.4\\"' which was '5.4\"' in my xml file.

Upvotes: 0

Views: 698

Answers (2)

mitchelangelo
mitchelangelo

Reputation: 899

Thanks for your help guys, I came to the solution of just doing a search and replace with sed on the original xml file before it is processed by xml2js with bash as this is part of a script, which worked out really well.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163262

Generally an XML-to-JSON library is going to assume that the data in the XML file is represented using normal XML conventions, that is, it will not contain JSON-style escape sequences. So it will assume that \" in the XML represents backslash followed by quotation mark, which is represented in JSON as \\\".

If you would like to consider using the xml-to-json() function in XPath 3.1, it has an option to mark the input XML with escaped="true" to indicate that JSON escape sequences are present in the XML. There are of course many other XML-to-JSON conversion libraries available, all with their own quirks, conventions, and restrictions.

Upvotes: 1

Related Questions