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); }}" } } }
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.
Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.
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.
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.
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