0

So got this JSON string,

{
    "Text": "<?xml version=\"1.0\"</pi>",
    "Type": "Checkin"
}

I need to deserialize it back to an object.

var returnedFileContent = '{"Text": "<?xml version=\"1.0\"</pi>","Type": "Checkin"}';
var test10 = JSON.parse(returnedFileContent);
console.log(test10)

Error:

Unexpected number in JSON at position 25

I know JS doesn't like the \" and it will work if the JSON string is:

{
    "Text": "<?xml version=\\\\\\" 1.0\\\\\\ "</pi>",
    "Type": "Checkin"
}

The problem is how to convert the \" to \\\".

Note: I have no control of the input JSON String.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 2
    Changing `\"` to `\'` seems to work, but if it does not parse, it is not JSON. – crashmstr Oct 21 '21 at 21:29
  • Also, is the Text _actually_ `""` ? That's not valid XML, either. – msanford Oct 21 '21 at 21:31
  • 3
    And sometimes, the right answer is to have some other person or team fix their stuff. – crashmstr Oct 21 '21 at 21:31
  • Does this answer your question? [How to escape a JSON string containing newline characters using JavaScript?](https://stackoverflow.com/questions/4253367/how-to-escape-a-json-string-containing-newline-characters-using-javascript) – Balastrong Oct 21 '21 at 21:31
  • msanford, it is not valid XML. You are right! I am just simplified the string to make a case here. crashmstr, this JSON string actually can be parsed and is valid in C#. And this JSON is generated from one of our C# library. @Balastrong, yes, the top answer in that post looks like a good workaround. I will give it a try. Thank you for all the suggestions! – Robert Lin Oct 21 '21 at 21:54

0 Answers0