Home > database >  Bad control character in string literal in JSON at position 197 error
Bad control character in string literal in JSON at position 197 error

Time:12-24

Bad control character in string literal in JSON at position 197 error is what i get when I try to parse this json string:

var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\r\n\r\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\r\n\r\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]');

And i really don't know what am I doing wrong.

I know for sure, that if I try to parse the json without a linebreak it works just fine, but when I try to have a content with a line break it just doen't work. I am not sure what the problem is here.

CodePudding user response:

The error message 'Bad control character in string literal' typically indicates that there is an invalid character in the string that you are trying to parse as JSON.

In this case, it looks like the problem is caused by the presence of the '\r' (carriage return) and '\n' (new line) characters in the 'content' property of the first object in the array. These characters are not allowed in JSON strings, and must be removed or escaped before the string can be parsed successfully.

To fix this error, you can use the String.replace() method to remove the '\r' and '\n' characters from the 'content' property:

var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison. Charles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour. He spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]'.replace(/[\r\n]/g, ''));

Alternatively, you can escape the '\r' and '\n' characters by adding a backslash before them:

var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\\r\\n\\r\\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\\r\\n\\r\\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]');
Either of these approaches should allow you to parse the JSON string successfully.

CodePudding user response:

The issue is you have to escape your new line character with another slash, if you works for you, you could upvote this answer eg \r\n

var obj = JSON.parse('[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\\r\\n\\r\\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\\r\\n\\r\\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]')

CodePudding user response:

You just have to write double \\

   var obj = JSON.parse(`[{"name":"Charles freed from Nepal jail","content":"A French serial killer known as The Serpent, convicted of several tourist murders in Asia in the 1970s, has been released from a Nepalese prison.\\r\\n\\r\\nCharles Sobhraj, 78, was freed after a court ruled in favour of his age and good behaviour.\\r\\n\\r\\nHe spent 19 years in jail in Nepal for killing two North Americans in 1975.","id":"1"}]`);
  • Related