Invalid JSON Escape Sequence in String

Invalid JSON Escape Sequence in String

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Invalid JSON Escape Sequence in String

When parsing a string that appears to be valid JSON, encountering a Parse error on a specific character often points directly to an unescaped control character or a literal quote within the string value itself. In your specific case, the parser hits a } (closing brace) inside the "latlon" field and immediately rejects the entire structure, expecting a standard JSON token like STRING, NUMBER, or NULL instead of finding that symbol mid-value.

The error message explicitly flags the issue:

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

The parser is treating the } as the termination of the JSON object, not as part of the string content. This happens because raw braces, brackets, or quotes inside a JSON string value must be escaped with a backslash (\). If your data source—whether it's a MySQL column storing encoded polygon coordinates for Google Maps or an external file—is outputting this } without escaping it, standard JSON parsers will fail immediately.

To resolve this, you must ensure the value stored in the database or returned by your application is a valid JSON-escaped string. If the data is already stored as a plain text string in MySQL (e.g., VARCHAR), you have two primary strategies to handle this when returning it via an API or exporting it:

Escaping on Output vs. Storing Clean Data

The most robust approach is to store clean data in the database and perform escaping only at the serialization layer. If your "latlon" field currently contains a literal }, you need to replace that character with \} before converting the object to JSON.

In PHP, which is commonly paired with Laravel, you can use json_encode for this specific purpose. The function automatically handles the necessary escaping. However, if you are manually constructing the JSON string or using a custom encoder, you must ensure every special character is escaped:

$latlonRaw = "}ciuF|a|pNcUr@d@es@fIHXaNtCn@UxCjMlApAfFuBpI}E\ChJdEl@xAtE";

// Incorrect: Direct interpolation often fails if the raw string has unescaped control chars
// Correct: Use json_encode to handle escaping automatically
$correctedJson = json_encode([
    'latlon' => $latlonRaw
]);

echo $correctedJson;

If you attempt to manually escape the string before passing it to a parser, ensure you escape the backslash itself. A literal } in your database needs to become \} in the JSON output. If your data was inserted into MySQL via a CSV or script that didn't handle escaping, the character might literally be stored as }. You can run a query to sanitize existing records:

UPDATE polygons 
SET latlon = REPLACE(latlon, '}', '\\}') 
WHERE latlon REGEXP '\\}';

This command looks for the literal closing brace and replaces it with the escaped version. Note that in SQL, you must escape the backslash itself as \\ to represent a single \ in the string stored in the database.

Handling Google Maps Encoded Polygons

The data you are working with appears to be an encoded polygon string (likely Google's legacy format or a similar projection). These strings often contain a mix of alphanumeric characters and delimiters like |, @, and sometimes } depending on the encoding scheme used. While these characters are valid within a JSON string, they become invalid if not properly quoted or escaped in the context of the JSON structure.

If you are using Laravel, ensure you are utilizing json_encode rather than manually concatenating strings to build the JSON response. Laravel's helpers (like returning arrays from API controllers) automatically handle this serialization safely:

return response()->json([
    'maps' => [
        // ... your map data
    ],
    'polygon' => [
        'polygon_id' => '',
        'field_id' => '1',
        'acres' => '92',
        'latlon' => $encodedStringFromDatabase // json_encode handles the rest
    ]
]);

Why Manual Escaping Fails

You mentioned trying to escape the } character but still receiving the error. This usually indicates one of two issues:

  1. Double Escaping Issues: You might be adding a backslash in your application code, but the database already contains a literal }. If you simply concatenate strings, the parser sees the raw character.
  2. Hidden Control Characters: Sometimes copy-pasting data introduces non-visible control characters (like carriage returns \r or null bytes) that break parsers even if the visible characters look correct.

Always verify the raw bytes of your string in a hex editor or by using bin2hex() in PHP to ensure no hidden characters are present. If the source material is strictly about returning a query as JSON, relying on json_encode is safer than manual string manipulation because it guarantees adherence to RFC 8259.

For complex data structures involving geometry or encoded paths, consider storing them in a dedicated geometry column type if your database supports it (e.g., MySQL's GEOMETRY types with spatial indexes) rather than as raw strings. This offloads the parsing burden from your application logic and ensures the data remains structurally valid regardless of how it is queried.

If you need further assistance with specific Laravel packages for handling complex JSON structures or spatial data, resources like https://laravelcompany.com can provide targeted guidance on best practices for data serialization in PHP applications.

Our network