php json_encode & jquery parseJSON single quote issue

php json_encode & jquery parseJSON single quote issue

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Trap of Single Quotes in json_encode and jQuery

When you encounter a JSON parsing error in the browser console, your instinct might be to inspect the network tab or check for syntax errors in your JavaScript. However, there is a specific edge case involving single quotes that often trips up developers working with json_encode in PHP and jQuery.parseJSON. The scenario looks innocent enough: you have an array containing a name like "Vincent D'Onofrio", you serialize it to JSON, and then attempt to deserialize it back using jQuery. Suddenly, the console throws an Uncaught SyntaxError: Unexpected identifier.

The root cause lies in how PHP's json_encode handles strings versus how JavaScript parses them. When you run $data = json_encode($var) where $var contains a string with a single quote, PHP generates valid JSON output by default. However, the resulting string looks like this:

{"casts":["Matthew Modine","Adam Baldwin","Vincent D'Onofrio"],"year":1987}

To the naked eye, this appears perfectly formatted. But when you embed this string directly into a JavaScript variable using an HTML template literal or by echoing it into a script tag like var obj = jQuery.parseJSON('<?=$data?>');, the browser interprets the single quote inside the JSON value as a syntax delimiter for your JavaScript string itself.

In the example above, the browser sees:

"Vincent D'Onofrio"

as a broken string because the single quote closes the JavaScript string prematurely. The parser then encounters Onofrio"> (where the closing quote of the JSON value is missing from its perspective) and throws a syntax error.

This explains why adding a backslash to the PHP output—changing it to "Vincent D'\\Onofrio"—fixes the issue. By escaping the single quote, you tell the JavaScript parser that this character is part of the string data, not a delimiter. However, relying on manual string manipulation or "dirty" hacks like replacing characters before encoding is generally a bad practice. It introduces unnecessary complexity and potential for error.

The solution is to ensure that the JSON string being passed to jQuery.parseJSON is properly escaped within the PHP context so it remains a valid single-line string when sent to the client. The most robust approach is to use json_encode with specific options or to ensure the variable is treated as raw data.

In modern development, relying on jQuery.parseJSON is often discouraged because it can parse non-standard JSON formats that native JSON.parse rejects. If you are using jQuery, you must explicitly escape the $data variable before embedding it into the JavaScript block. You can achieve this by wrapping the output in PHP's htmlspecialchars with specific flags:

<script>
    var jsonStr = <?php echo htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); ?>;
    var obj = JSON.parse(jsonStr); // Prefer native JSON.parse over jQuery.parseJSON
    console.log(obj);
</script>

Using htmlspecialchars with the ENT_QUOTES flag converts both single and double quotes in the PHP variable to their HTML entities (&#039; for single quotes). This ensures that when the browser renders the script tag, it sees a literal apostrophe character rather than a string terminator. While this changes the internal representation of the data (storing the entity instead of the raw character), JSON.parse handles these entities correctly if they are not double-encoded.

If you prefer to keep the raw characters in the JSON payload without converting them to HTML entities, you must ensure the PHP variable is output as a single-quoted string that JavaScript can safely interpolate. The safest pattern involves echoing the JSON directly inside parentheses or ensuring the surrounding context prevents the browser from interpreting the inner quotes as delimiters.

For instance, if $data contains the raw JSON string, you can pass it through json_encode again with the JSON_UNESCAPED_UNICODE flag only if needed, but crucially, you must avoid wrapping the output in PHP tags that might introduce extra characters. A common pitfall is echoing the variable directly inside a double-quoted PHP string context which can alter escape sequences unexpectedly.

Ultimately, the error is not a bug in the browser; it is a mismatch between how PHP generates JSON and how JavaScript parses inline strings. By understanding that single quotes are valid delimiters in JavaScript but data characters in JSON, you can choose the right escaping strategy—whether using htmlspecialchars for simplicity or carefully constructing your template literals to maintain data integrity.

Our network