Reputation: 1091
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
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
Reputation: 530
the accepted answer not work for me. I use
json = json.replace(/\\/g,"");
let arrayObj = JSON.parse(json);
Upvotes: 8
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
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
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
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