The Python Oracle

Single vs double quotes in JSON

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Single Vs Double Quotes In Json
00:23 Accepted Answer Score 256
00:35 Answer 2 Score 72
00:48 Answer 3 Score 210
00:59 Answer 4 Score 19
01:25 Thank you

--

Full question
https://stackoverflow.com/questions/4162...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #json

#avk47



ACCEPTED ANSWER

Score 256


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




ANSWER 2

Score 210


you can use ast.literal_eval()

>>> import ast
>>> s = "{'username':'dfdsfdsf'}"
>>> ast.literal_eval(s)
{'username': 'dfdsfdsf'}



ANSWER 3

Score 72


You can dump JSON with double quote by:

import json

# mixing single and double quotes
data = {'jsonKey': 'jsonValue',"title": "hello world"}

# get string with all double quotes
json_string = json.dumps(data) 



ANSWER 4

Score 19


demjson is also a good package to solve the problem of bad json syntax:

pip install demjson

Usage:

from demjson import decode
bad_json = "{'username':'dfdsfdsf'}"
python_dict = decode(bad_json)

Edit:

demjson.decode is a great tool for damaged json, but when you are dealing with big amourt of json data ast.literal_eval is a better match and much faster.