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 usenull
,true
andfalse
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 hasNone
,True
andFalse
(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
Install a Module Using Pip for Specific Python Version
Perform Commands Over Ssh with Python
Pandas: Filter Rows of Dataframe with Operator Chaining
Convert Pandas Timezone-Aware Datetimeindex to Naive Timestamp, But in Certain Timezone
How to Detach Matplotlib Plots So That the Computation Can Continue
"Fire and Forget" Python Async/Await
Python 3 Importerror: No Module Named 'Configparser'
How to Use a Python Script in the Command Line Without Cd-Ing to Its Directory? Is It the Pythonpath
How to Convert SQLalchemy Row Object to a Python Dict
String Replace Doesn't Appear to Be Working
How Slow Is Python's String Concatenation VS. Str.Join
How to Do a Recursive Sub-Folder Search and Return Files in a List