0

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.

mitchelangelo
  • 851
  • 4
  • 16
  • 42
  • Can you show some more context here? – Brad Jan 16 '19 at 00:01
  • Imagine a json response from xml2js which is broken because of one escape character which was inserted by xml2js next to an already existing escape charater which was originally present in the xml file, the problem is xml2js inserted an escape character during conversion to json which breaks the returned json file. Is there anyway to disable automatically inserting escape characters? – mitchelangelo Jan 16 '19 at 00:13
  • Can you show some of this input data and the actual output data? – Brad Jan 16 '19 at 00:50

2 Answers2

1

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

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.

mitchelangelo
  • 851
  • 4
  • 16
  • 42