jQuery.parseJSON single quote vs double quote
That's because double quotes is considered standard while single quote is not. This is not really specific to JQuery, but its about JSON standard. So irrespective of JS toolkit, you should expect same behaviour.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
Update
Or perhaps its a duplicate of jQuery single quote in JSON response
jQuery.parseJSON throws “Invalid JSON” error due to escaped single quote in JSON
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:
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.
Is there any difference in JSON Key when using single quote and double quote?
In JSON only double quotes are valid.
You can find the standard on JSON.org
A value can be a string in double quotes, or a number, or true or
false or null, or an object or an array. These structures can be
nested.
In other words, no strings in single quotes.
Parse json string with double quotes in jquery
In Javascript object literals, quoting the property name is optional if it's a valid identifier. So you can write either:
var your_json = { firstName: 'James', lastName: 'Smith' };
or
var your_json = { "firstName": "James", lastName: "Smith" };
In JSON, double quotes are required. The equivalent JSON string would be:
var your_json_string = '{"firstName": "James", "lastName": "Smith"}';
or:
var your_json_string = "{\"firstName\": \"James\", \"lastName\": \"Smith\"}";
You can then parse this with:
var your_json = $.parseJSON(your_json_string);
Jquery: error in parsing valid Json string with double quotes
The problem was with JSON-encoded object, injected into script as string and then JSON-decoded.
<script>
var data='{"defaultLocale":"en","answers":[{"text":"\"xxxx"} ... }';
</script>
So, as long as JSON-encoded object string has a valid javascript syntax (as intended, obviously), it can be injected directly, without string -> JSON-decode step:
<script>
var data = {"defaultLocale":"en","answers":[{"text":"\"xxxx"} ... };
</script>
Related Topics
Accessing Private Member Variables from Prototype-Defined Functions
Difference Between Knockout View Models Declared as Object Literals VS Functions
What Are the Differences Between Deferred, Promise and Future in JavaScript
JavaScript Get Xpath of a Node
What Does [].Foreach.Call() Do in JavaScript
Dynamic Keys for Object Literals in JavaScript
How to Make Div Fixed After You Scroll to That Div
How to Find the Width of a Div Using Vanilla JavaScript
Load HTML File Contents to Div [Without the Use of Iframes]
Make HTML Text Input Field Grow as I Type
JavaScript Filter Array Multiple Conditions
How to Get Current Value of Rxjs Subject or Observable
How to Decode Jwt Token in JavaScript Without Using a Library
Determine Original Size of Image Cross Browser
How to Query Mongodb Objectid by Date
Jquery.Parsejson Single Quote VS Double Quote
Execcommand() Is Now Obsolete, What's the Alternative
Date Constructor Returns Nan in Ie, But Works in Firefox and Chrome