Single vs double quotes in JSON
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping
--
Chapters
00:00 Question
00:31 Accepted answer (Score 230)
00:47 Answer 2 (Score 183)
01:02 Answer 3 (Score 68)
01:19 Answer 4 (Score 21)
03:06 Thank you
--
Full question
https://stackoverflow.com/questions/4162...
Accepted answer links:
[JSON syntax]: http://www.json.org/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping
--
Chapters
00:00 Question
00:31 Accepted answer (Score 230)
00:47 Answer 2 (Score 183)
01:02 Answer 3 (Score 68)
01:19 Answer 4 (Score 21)
03:06 Thank you
--
Full question
https://stackoverflow.com/questions/4162...
Accepted answer links:
[JSON syntax]: http://www.json.org/
--
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.decodeis a great tool for damaged json, but when you are dealing with big amourt of json dataast.literal_evalis a better match and much faster.