5

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.

Norbi
  • 67
  • 1
  • 6
  • 1
    Does this answer your question? [Are multi-line strings allowed in JSON?](https://stackoverflow.com/questions/2392766/are-multi-line-strings-allowed-in-json) – phuzi Dec 23 '22 at 11:25
  • You will need to encode/escape the carriage return ```\r``` character inside the string value as they are not allowed as you currently have them. – phuzi Dec 23 '22 at 11:26
  • 1
    So what's really happening here? Why are you using a JSON string in your Javascript source code? Is it being injected into the code by a server process? If so, the fix is to ensure that the injected JSON string is correctly escaped when it is injected, not to try and fix subsequently in the consuming JS. – spender Dec 23 '22 at 11:44
  • This code is redundant, it doesn't make a lot of sense to enter a JSON string with the sole purpose of producing a JavaScript object. Just type that same input as raw object and skip the `JSON.parse()` part. If it's just a simplification to illustrate the problem, it might be too simplified since `\r\n` are escape sequences in many languages. – Álvaro González Dec 24 '22 at 10:57

2 Answers2

3

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"}]`);
Haim Abeles
  • 982
  • 6
  • 19
1

The issue is you have to escape your new line character with another slash,

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"}]')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Davy King
  • 27
  • 8
  • and how should I do it using php? because I get my json from: ```php header("Content-Type: application/json"); include "database_config/db_config.php"; $connection = mysqli_connect($db_config["server"], $db_config["login"], $db_config["password"], $db_config["database"]); $query = "SELECT * FROM articles"; $result = mysqli_query($connection, $query); $all_page = mysqli_fetch_all($result, MYSQLI_ASSOC); $to_js = json_encode($all_page); print($to_js); ``` – Norbi Dec 23 '22 at 11:24
  • can i see your mysql database. I believe the format you are gettting your results is wrong. – Davy King Dec 23 '22 at 11:25
  • I really thought...I answered this correctly – Davy King Dec 23 '22 at 12:39