P.Henderson
P.Henderson

Reputation: 1091

How to fix an escaped JSON string (JavaScript)

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?

Upvotes: 44

Views: 81375

Answers (6)

Pier-Luc Gendreau
Pier-Luc Gendreau

Reputation: 13814

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' }

Upvotes: -1

Kenneth Chan
Kenneth Chan

Reputation: 530

the accepted answer not work for me. I use

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

Upvotes: 8

Iman  Mohamadi
Iman Mohamadi

Reputation: 6829

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);

Upvotes: -2

sadrzadehsina
sadrzadehsina

Reputation: 1349

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);

Upvotes: 1

user2864740
user2864740

Reputation: 61875

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.

Upvotes: 55

cyril
cyril

Reputation: 1005

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...

Upvotes: -4

Related Questions