Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve JSON.parse: bad control character in string literal, in this code [closed]

Tags:

I'm a beginner at JSON document, this is my code so please help me to solve this error.

{    "_id" : "_design/utilisateur",    "_rev" : "1-967a00dff5e02add41819138abb3284d",    "views" : {     "tous" : {          "map" : "function(doc){if (doc.role=='utilisateur') {emit (doc._id,        [['t0',doc.distancet0],['t1',doc.distancet1],['t2',doc.distancet2],['t3',doc.distancet3],    ['t4',doc.distancet4]])}}"          },       "3500" : {         "map" : "function(doc) {if (doc.role=='utilisateur' &&     doc.distancet0<3500) {emit(doc._id,doc.distancet0)}}"         },          "distancetot" : {                 "map" : "function(doc) {var somme= Math.abs(doc.distancet0-    doc.distancet1); if(doc.role=='utilisateur'){    emit(doc._id, somme); }}"                           }     }             } 
like image 845
user2553396 Avatar asked Jul 05 '13 10:07

user2553396


People also ask

How do you handle JSON parsing error?

The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

What error does JSON parse () throw when the string to parse is not valid JSON?

Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.

What is JSON control character?

The JSON specification states that control characters that must be escaped are only with codes from U+0000 to U+001F: 7. Strings The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.


1 Answers

The error message is telling you that you have a control character within a string literal, for instance, character code 8 or 10 or 13 or anything below 32 (a space).

The JSON definition tells us that you cannot have literal control characters in string literals, you must use an escape sequence such as \b, \r, \n, or \uXXXX where XXXX is a hex code for a Unicode "code point" (character).

So for instance, pretend the following is in a file (or other data stream):

{     "property": "value with an invalid control character in it" } 

That's invalid JSON, the string literal starting with "value has at least one control character in it (the line break, might be one or two control characters depending on the OS).

This is how we would fix it:

{     "property": "value with an valid\nescape sequence in it" } 

Note the \n where the line break used to be.

You can use http://jsonlint.com to validate JSON, it's quite good at pointing out where the error is.


Re your edit: It is indeed a line break causing the problem:

"distancetot": {     "map": "function(doc) {var somme= Math.abs(doc.distancet0-    doc.distancet1); if(doc.role=='utilisateur'){  Error is here -------------------------------------------------------------------------------------------------^ 

The line break after if(doc.role=='utilisateur'){ is an invalid control character, just as in my example above.

like image 190
T.J. Crowder Avatar answered Sep 16 '22 15:09

T.J. Crowder