Why does XML to JSON insert a backslash in front of my forward slash and how can I stop it?

Not applicable

Input XML fragment:

<organizationStateParty>Acme (ME / 74)</organizationStateParty>

Resultant JSON:

"organizationStateParty":"Acme (ME \/ 74)"

Online tools don't do it.

Also, what are the valid values for the Format property on this policy? It's not documented and its just a string in the Schema.

Thanks, Kurt

Solved Solved
1 3 32.1K
1 ACCEPTED SOLUTION

Dear @Kurt Kanaskie ,

Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response.

You have to use JSON.parse to parse that JSON string into a JSON object.

For example, below url will have backward slashes if it's a raw JSON string. The same url if you convert into object using JSON.parse or install chrome browser plugin like JSON viewer you will never see them.

754-screen-shot-2015-07-13-at-101144-pm.png

753-screen-shot-2015-07-13-at-101130-pm.png

Cheers,

Anil Sagar

View solution in original post

3 REPLIES 3

Dear @Kurt Kanaskie ,

Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response.

You have to use JSON.parse to parse that JSON string into a JSON object.

For example, below url will have backward slashes if it's a raw JSON string. The same url if you convert into object using JSON.parse or install chrome browser plugin like JSON viewer you will never see them.

754-screen-shot-2015-07-13-at-101144-pm.png

753-screen-shot-2015-07-13-at-101130-pm.png

Cheers,

Anil Sagar

Some more discussion regarding same here..

Hi Anil,

Thanks for your response, I must be missing something obvious.

In my scenario, my node app is returning results from a backend DB. I convert that JSON result into XML using JSON-to-XML, run an XSLT, then convert back using XML-to-JSON. I don't want the string to be modified.

I understand if the JSON had a value that included angle brackets, JSON-to-XML converts that to "&lt;uhoh&ampgt;" in the XML text and XML-to-JSON converts that back to the angle brackets (at least in the online tool: http://www.utilities-online.info/xmltojson). I see how those are equivalent.

Consider this in node:

$ node --version
v0.10.31
> var tmp = {
...   "root": {
.....     "el1": "name (code / number)",
.....     "el2": "http://www.something",
.....     "el3": "<uhoh> this could be trouble"
.....   }
... }

> tmp
{ root: 
   { el1: 'name (code / number)',
     el2: 'http://www.something',
     el3: '<uhoh> this could be trouble' } }

> JSON.stringify(tmp)
'{"root":{"el1":"name (code / number)","el2":"http://www.something","el3":"<uhoh> this could be trouble"}}'

> JSON.parse(JSON.stringify(tmp))
{ root: 
   { el1: 'name (code / number)',
     el2: 'http://www.something',
     el3: '<uhoh> this could be trouble' } }

Seems to deal with the slash just fine.

Thanks, Kurt