14

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"
        }
    ]
}
1
  • That error suggest there's a problem earlier in the json, not on that line.. Commented Jan 3, 2013 at 12:46

1 Answer 1

18

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Playing around this with myself, it took me 3 slashes to get it to decode in php. ie. \\\C
I figured it out by using: <pre>$tmp = str_replace('"', '\"', $maps['lat_lon']); $tmp = str_replace('\\', '\\\\', $tmp);</pre> Are there any other special characters I need to be escaping that could be in a json string?
Are you concatenating your own string or are you using json_encode?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.