Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid JSON Escape Sequence in String

Tags:

json

string

php

I am working with a MySQL db that has encoded polygon for google maps. When I try to return the query as json, jsonlint complains.. I am not sure why its complaining , I did try escaping the "}" in the latlon but still get the same error.

Parse error on line 20:
...          "latlon": "}ciuF|a|pNcUr@d@es@
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

My json is:

{
    "maps": [
        {
            "group_id": "0",
            "user_id": "113",
            "group_name": "",
            "note": "",
            "field_id": "",
            "field_name": "West Pasture",
            "field_notes": "",
            "date_created": "12/31/2012",
            "acres": ""
        }
    ],
    "polygon": [
        {
            "polygon_id": "",
            "field_id": "1",
            "acres": "92",
            "latlon": "}ciuF|a|pNcUr@d@es@fIHXaNtCn@UxCjMlApAfFuBpI}E\ChJdEl@xAtE"
        }
    ]
}
like image 613
Dennis Avatar asked Jan 03 '13 12:01

Dennis


People also ask

How do you fix an invalid escape character in a string?

Resolving The Problem Regex r = new Regex("\(HelloWorld\)"); which generates the Invalid escape sequence error. To re-iterate, the backslash character can be used as an escape sequence for a regular expression in a recognition property or a verification point.

How do you escape a JSON string?

JSON is pretty liberal: The only characters you must escape are \ , " , and control codes (anything less than U+0020). This structure of escaping is specific to JSON. You'll need a JSON specific function. All of the escapes can be written as \uXXXX where XXXX is the UTF-16 code unit¹ for that character.

What is escape sequence in JSON?

If you have to use special character in your JSON string, you can escape it using \ character. See this list of special character used in JSON : \b Backspace (ascii code 08) \f Form feed (ascii code 0C) \n New line \r Carriage return \t Tab \" Double quote \\ Backslash character.

What is invalid escape sequence?

[Solved] Invalid escape sequence (valid ones are \b \t \n \f \r \' \”; \\ ) May 19, 2018 Saurabh Gupta Leave a comment. “Invalid escape sequence” is most common error at compile time while using Regular Exception or defining File Path while file handling for reading and writing files.


1 Answers

The problem is that there is a slash before the C which is not a valid escape sequence.

"}ciuF|a|pNcUr@d@es@fIHXaNtCn@UxCjMlApAfFuBpI}E\ChJdEl@xAtE"

JSON.parse('"\\C"');

This will give you a syntax error because it is trying to parse the string \C. If you want a literal \ in your property's value, you need to escape it.

"latlon": "}ciuF|a|pNcUr@d@es@fIHXaNtCn@UxCjMlApAfFuBpI}E\\ChJdEl@xAtE"

The relevant section from the official grammar:

string
    ""
    " chars "
chars
    char
    char chars
char
    any-Unicode-character-
        except-"-or-\-or-
        control-character
    \"
    \\
    \/
    \b
    \f
    \n
    \r
    \t
    \u four-hex-digits 
like image 100
Dennis Avatar answered Sep 23 '22 00:09

Dennis