Consider I have the following string:
{
"{\n <<<-- error
\"SomeKey\": {\n \"somevalue\": \"test\",\n,
\"AnotherKey\": \"Long string should be here \n another line break here \n and another line here \"
}
}
When you try to parse this string with JSON.parse, it throws an error that points to the first line break. Is there any way to get rid of the line breaks without removing \n that is not within quotation marks.
Strip \n
from the JSON string and do JSON.parse
var json_data = "{\n \"Fullname\": \"Alex Johnson\",\n \"FirstName\": \"Alex\", \n \"LastName\": \"Johnson\"\n }";
var obj = JSON.parse(json_data.replace(/\r?\n|\r/g, ''));
console.log(obj);
use JSON.stringify
and remove the line break;
var json = JSON.stringify(jsonData);
json = json.replace(/\\n/g, '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With