Keywords: jQuery | JSON | Escaped Single Quotes
Abstract: This paper investigates the cause of jQuery.parseJSON throwing an "Invalid JSON" error when processing JSON strings containing escaped single quotes. By analyzing the differences between the official JSON specification and JavaScript implementations, it clarifies the handling rules for single quotes in JSON strings. The article details the underlying JSON parsing mechanisms in jQuery, compares compatibility across various libraries, and provides practical solutions and best practices for development.
String Escape Rules in the JSON Specification
According to the official JSON specification (json.org), strings must be defined using double quotes, and only specific characters are allowed to be escaped. The state machine diagram explicitly shows that within strings, only the following characters can be escaped with a backslash: double quote ("), backslash (\\), slash (\/), backspace (\b), form feed (\f), newline (\n), carriage return (\r), and tab (\t). The single quote character (') does not need and is not allowed to be escaped in JSON strings, as string delimitation relies solely on double quotes. This design adheres to JSON's core principles: minimalism, portability, textuality, and being a subset of JavaScript, reducing ambiguity across implementations.
Implementation Mechanism of jQuery.parseJSON
The jQuery.parseJSON method attempts to use the browser's native JSON.parse implementation first when parsing JSON strings. If unsupported, it falls back to a backup parser based on json2.js. Both implementations strictly follow the JSON specification, thus not accepting escaped single quotes. For example, given the string data = "{ \"status\": \"success\", \"newHtml\": \"Hello \\'x\" }";, calling $.parseJSON(data) throws an "Invalid JSON" error because \' is non-compliant. In contrast, eval("x = " + data) executes successfully, as JavaScript's eval is more lenient, allowing single-quoted strings and escapes, but this poses security risks and is not recommended.
Compatibility Analysis Across JSON Implementations
Despite the official prohibition, some JSON implementation libraries offer more permissive parsing. For instance, Douglas Crockford's org.json Java library accepts single-quoted strings and escaped single quotes in the JSONTokener.nextString method, with source code showing handling logic similar to double quotes. However, this leniency is non-standard, and mainstream implementations like browser-built-in JSON parsers and libraries relied on by jQuery typically adhere strictly to the specification, leading to compatibility issues. Developers should avoid depending on non-standard features when transmitting JSON data across platforms or libraries to ensure interoperability.
Solutions and Best Practices
To prevent parsing errors caused by single quotes, it is recommended to ensure that single quotes in string values are not escaped when generating JSON on the server side. For example, in the sample, the correct format should be { "status": "success", "newHtml": "Hello 'x" }, with the single quote treated as a regular character. If the data source is uncontrollable, preprocess with string replacement on the client side, such as data.replace(/\\'/g, "'"), but be cautious of potential data corruption risks. Overall, adhering to the JSON specification and testing parsing behavior in different environments is key to ensuring stability.