Single VS Double Quotes in JSON

Single vs double quotes in JSON

JSON syntax is not Python syntax. JSON requires double quotes for its strings.

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

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.

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.

Single quotes within json value

Use a backslash to escape the character:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);

Python json difference between single quotes and double quotes

RFC7159 defining JSON stipulates

A string begins and ends with quotation marks.

i.e. ", it does not allow ' unlike python

Single versus double quotes in json loads in Python

Use the proper tool for the job, you are not parsing JSON but Python, so use ast.literal_eval() instead:

>>> import ast
>>> ast.literal_eval('["a", "b", "c"]')
['a', 'b', 'c']
>>> ast.literal_eval("['a', 'b', 'c']")
['a', 'b', 'c']
>>> ast.literal_eval('["mixed", \'quoting\', """styles"""]')
['mixed', 'quoting', 'styles']
  • JSON documents always use double quotes for strings, use UTF-16 for \uhhhh hex escape syntax, have {...} objects for key-value pairs with keys always strings and sequences are always [...] lists, and use null, true and false values; note the lowercase booleans. Numbers come in integer and floating point forms.

  • In Python, string representations can use single and double quotes, Unicode escapes use \uhhhh and \Uhhhhhhhh forms (no UTF-16 surrogate pairs), dictionaries with {...} display syntax can have keys in many different types rather than just strings, sequences can be lists ([...]) but can also use tuples ((...)), or you could have other container types still. Python has None, True and False (Titlecase!) and numbers come in integers, floats, and complex forms.

Confusing one with the other can either lead to parse errors or subtle problems when decoding happened to succeed but the data has been wrongly interpreted, such as with escaped non-BMP codepoints such Emoji. Make sure to use the right method to decode them! And in most cases when you do have Python syntax data someone actually used the wrong method of encoding and only accidentally produced Python representations. See if the source needs fixing in that case; usually the output was produced by using str(object) where json.dumps(obj) should have been used instead.



Related Topics



Leave a reply



Submit