Although your JSON string is valid JSON.parse
is likely to throw error. This is because JSON.parse
cannot parse some special characters that are \n
, \t
, \r
and \f
.
You need to escape these special characters for before passing JSON string to JSON.parse
function.
Here is a function that takes a JSON string and escapes the special characters:
function escapeSpecialChars(jsonString) {
return jsonString.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\t/g, "\\t")
.replace(/\f/g, "\\f");
}
return jsonString.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\t/g, "\\t")
.replace(/\f/g, "\\f");
}