How can I parse JSON with double quotes?

Viewed 34

I'm having an issue when trying to parse an JSON returned by a service

the JSON returned looks something like this:

"{\\\"Description\\\":\\\"\\\\\\\"Hello world\\\\\\\" How are you?\\\"}"

And when I execute the JSON.parse I get the following error:

Uncaught SyntaxError: Expected property name or '}' in JSON at position 1

The expected result of the description value should be (Keeping the double quotes surrounding the "Hello world":

{
  Description: `"Hello world" How are you?`,
}

Am I missing anything?

Thanks in advance

1 Answers

to fix your string to json, you can use a replace string function

var jsonString="{\\\"Description\\\":\\\"\\\\\\\"Hello world\\\\\\\"\\\"}";
jsonString=jsonString.replaceAll("\\","").replaceAll("\"\"","\"");

console.log(jsonString);

`{"Description":"Hello world"}`

or if you need an escaped string ( but I don't think so)

jsonString= JSON.stringify(jsonString);

"{\"Description\":\"Hello world\"}"

test

var jsonObject = JSON.parse(jsonString); //OK
Related