How to fix an escaped JSON string (JavaScript)
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.
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);
the accepted answer not work for me. I use
json = json.replace(/\\/g,"");
let arrayObj = JSON.parse(json);