json parse error with double quotes

json parse error with double quotes

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

Stefan Bogdanescu

Founder & Senior Architect

JSON Parse Error with Double Quotes

When working with JavaScript and PHP, one of the most common sources of frustration is a JSON.parse error that occurs specifically because of double quotes inside a string value. This isn't a bug in the parser; it is a direct result of how escape sequences are handled during serialization and deserialization.

Consider this scenario where you have a JSON string representing data with escaped quotes:

var testJson = '{"result": ["lunch", "\"Show\""] }'; 
var tags = JSON.parse(testJson); 
alert(tags.result[1]);

This code throws a parse error. The issue lies in the content of testJson. Inside that string literal, the value "\"Show\"" contains backslashes followed by double quotes. In a raw JavaScript string, a single backslash (\) is an escape character. Therefore, when you write \" inside the string definition, JavaScript interprets this as an escaped quote character within the source code itself. When that string is passed to JSON.parse, the parser sees a literal backslash followed by a quote, which is valid JSON syntax if the input string was meant to represent a raw string with quotes. However, if the source of this string came from PHP, something subtle went wrong regarding how the escaping layers stacked up.

The user notes that using double slashes (\\) in the JavaScript variable assignment fixes the issue:

var result = '{"result": ["lunch", "\\"Show\\""] }'; 
var tags = JSON.parse(result); 
alert(tags.result[1]);

Why does this work? It comes down to the layering of escape sequences. In JavaScript source code, you need \\ to represent a single backslash in the resulting string. When you write "\\\"Show\\\"", the JavaScript engine processes the first layer: it sees \\ and converts that into a single \. The result is a string containing \"Show\". This is exactly what JSON.parse expects when reading a JSON object that contains quoted strings inside its values.

The root cause described in the source material is often a mismatch between PHP's json_encode() function and JavaScript's expectations. When PHP serializes a string containing quotes, it outputs them as \". For example, if the PHP variable is 'Show', json_encode produces "\"Show\"". If you then take that raw output and treat it as a JavaScript string literal without proper handling, you end up with an extra layer of escaping or a missing layer, leading to the parse error.

To handle this situation correctly in a mixed PHP/JavaScript environment, you generally should not manually manipulate escape sequences using double slashes unless you are strictly building the JSON string from scratch within JavaScript. Instead, ensure that the data flow respects the standards:

  1. PHP Side: Always use json_encode() on your PHP arrays or objects. This function correctly escapes internal quotes for JSON format (producing \").
  2. JavaScript Side: If you are fetching this data via an API call (AJAX/Fetch), the response will already be a properly encoded string. You pass that response directly to JSON.parse(). Do not wrap it in additional string literals or manually add slashes.

The only time you need to manually adjust escape characters like using \\ is if you are constructing the JSON payload strictly within JavaScript code using template literals or concatenation, and you want to simulate a value that contains quotes. In those cases, you must double-escape for the JavaScript parser first, so the resulting string passed to JSON.parse matches the format expected by the JSON RFC.

Attempting to use eval() is not a viable solution here. While eval() can execute arbitrary code, it does not automatically fix malformed escape sequences; it simply executes the string as-is. If the string has invalid JSON structure due to mismatched escapes, eval will also throw a syntax error.

If you are building a Laravel application and dealing with API responses or frontend data integration, relying on the framework's built-in serialization is the safest path. The LaravelCompany guide on handling data structures emphasizes letting the framework manage the encoding standards rather than manually crafting strings, which often leads to these exact parsing conflicts. By avoiding manual string manipulation for JSON payloads and trusting json_encode and JSON.parse to handle their respective layers, you eliminate the need for ad-hoc double-slash fixes and ensure robust data interchange between server and client.

Our network