44

A remote server (out of my control) send a JSON string which has all fieldnames and values escaped. For example, when I do JSON.stringify(res), this is the result:

"{\"orderId\":\"123\"}"

Now when I do alert(res.orderId), it says undefined. I think it's because of the escaped "s. How do I fix this?

2
  • 1
    What's shown if you console.log(res)? See, I really fail to understand why do you stringify the result instead of parsing it. Commented Sep 8, 2014 at 9:33
  • give the code what fires the bug. Commented Sep 8, 2014 at 9:34

6 Answers 6

55

Assuming that is the actual value shown then consider:

twice_json = '"{\\"orderId\\":\\"123\\"}"'  // (ingore the extra slashes)
json = JSON.parse(twice_json)               // => '{"orderId":"123"}'
obj = JSON.parse(json)                      // => {orderId: "123"}
obj.orderId                                 // => "123"

Note how applying JSON.stringify to the json value (which is a string, as JSON is text) would result in the twice_json value. Further consider the relation between obj (a JavaScript object) and json (the JSON string).

That is, if the result shown in the post is the output from JSON.stringify(res) then res is already JSON (which is text / a string) and not a JavaScript object - so don't call stringify on an already-JSON value! Rather, use obj = JSON.parse(res); obj.orderId, as per the above demonstrations/transformations.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. For some reason all other Google APIs returned an object, but now this API returned a string that I had to JSON.parse() myself.
8

the accepted answer not work for me. I use

json = json.replace(/\\/g,"");
let arrayObj = JSON.parse(json);

5 Comments

Should mention why the answer didn't work for you (i.e. how it fails)
What is json here? There's no replace for the standard JSON object in js...
in some case. the string contain "\" symbol, so I need to trim them.
I recomend using str.replace(/\\"/g, '"') here
This just removes any backslashes, so \\t will be replaced with t in the output, probably not what you want..
1

It's actually an object that you execute JSON.stringufy on it.

var jsonString = "{\"orderId\":\"123\"}";
var jsonObject = JSON.parse(jsonString);

console.log(jsonObject.orderId);

Or you do simple than that

var jsonObject = JSON.parse("{\"orderId\":\"123\"}");
console.log(jsonObject.orderId);

Comments

-1

The suggested solutions provide an answer to the specific problem in the question but can't fix overly escaped JSON in a generic way.

Here's the solution I came up with:

const veryEscapedJsonString = "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"potato\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"salad\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""

function jsonParseUntilUnescaped(escapedJson) {
    const parsedJson = JSON.parse(escapedJson)
    const stringifiedParsedJson = parsedJson.toString();

    if (stringifiedParsedJson.includes('\"')) {
        return jsonParseUntilUnescaped(stringifiedParsedJson )
    }

    return parsedJson;
}

const json = jsonParseUntilUnescaped(veryEscapedJsonString);

// { potato: 'salad' }

Comments

-2

You can use JSON.parse, I don't really know what that API returns for you so I can't give you alternatives.

var res = "{\"orderId\":\"123\"}";
res = JSON.parse(res);
alert(res.orderId);

1 Comment

Ok, I'm going to double check with your solution.
-4

Do you really need to stringify your data can not juste use json.orderId ? you say send you a json string ? you have not to do stringify if your json is already in string.... if you have chrome debugger or another browser debugger you can see the type of your var...

2 Comments

No I don't, but I pasted the stringify here to show that the "'s are escaped. Normally you wouldn't see those backslashes with stringify. I think it's the reason why res.orderId returns undefined.
have you tried $.parsedJSON(yourjson) ? i repeat if it is already in string format it is normal you can not play with json data !sometimes following the way to send or retrieve the data you have to do it manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.