Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 181767  / 15 Years ago, tue, february 16, 2010, 12:00:00

I’m making requests to my server using jQuery.post() and my server is returning JSON objects (like { var: value, ... }). However, if any of the values contains a single quote (properly escaped like '), jQuery fails to parse an otherwise valid JSON string. Here’s an example of what I mean (done in Chrome’s console):



data = { status: success, newHtml: Hello \'x };
eval(x = + data); // { newHtml: Hello 'x, status: success }

$.parseJSON(data); // Invalid JSON: { status: success, newHtml: Hello 'x }


Is this normal? Is there no way to properly pass a single quote via JSON?


More From » jquery

 Answers
20

According to the state machine diagram on the JSON website, only escaped double-quote characters are allowed, not single-quotes. Single quote characters do not need to be escaped:



http://www.json.org/string.gif





Update - More information for those that are interested:






Douglas Crockford does not specifically say why the JSON specification does not allow escaped single quotes within strings. However, during his discussion of JSON in Appendix E of JavaScript: The Good Parts, he writes:




JSON's design goals were to be minimal, portable, textual, and a subset of JavaScript. The less we need to agree on in order to interoperate, the more easily we can interoperate.




So perhaps he decided to only allow strings to be defined using double-quotes since this is one less rule that all JSON implementations must agree on. As a result, it is impossible for a single quote character within a string to accidentally terminate the string, because by definition a string can only be terminated by a double-quote character. Hence there is no need to allow escaping of a single quote character in the formal specification.





Digging a little bit deeper, Crockford's org.json implementation of JSON for Java is more permissible and does allow single quote characters:




The texts produced by the toString methods strictly conform to the JSON syntax rules. The constructors are more forgiving in the texts they will accept:



...




  • Strings may be quoted with ' (single quote).




This is confirmed by the JSONTokener source code. The nextString method accepts escaped single quote characters and treats them just like double-quote characters:



public String nextString(char quote) throws JSONException {
char c;
StringBuffer sb = new StringBuffer();
for (;;) {
c = next();
switch (c) {

...

case '\':
c = this.next();
switch (c) {

...

case '':
case ''':
case '\':
case '/':
sb.append(c);
break;
...


At the top of the method is an informative comment:




The formal JSON format does not allow strings in single quotes, but an implementation is allowed to accept them.




So some implementations will accept single quotes - but you should not rely on this. Many popular implementations are quite restrictive in this regard and will reject JSON that contains single quoted strings and/or escaped single quotes.






Finally to tie this back to the original question, jQuery.parseJSON first attempts to use the browser's native JSON parser or a loaded library such as json2.js where applicable (which on a side note is the library the jQuery logic is based on if JSON is not defined). Thus jQuery can only be as permissive as that underlying implementation:



parseJSON: function( data ) {
...

// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}

...

jQuery.error( Invalid JSON: + data );
},


As far as I know these implementations only adhere to the official JSON specification and do not accept single quotes, hence neither does jQuery.


[#97555] Saturday, February 13, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Fri, Oct 16, 20, 00:00, 4 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Sun, Jul 28, 19, 00:00, 5 Years ago
;