Fix Python – Single vs double quotes in JSON

Question

Asked By – Bin Chen

My code:

import simplejson as json

s = "{'username':'dfdsfdsf'}" #1
#s = '{"username":"dfdsfdsf"}' #2
j = json.loads(s)

#1 definition is wrong

#2 definition is right

I heard that in Python that single and double quote can be interchangable. Can anyone explain this to me?

Now we will see solution for issue: Single vs double quotes in JSON


Answer

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

This question is answered By – Ignacio Vazquez-Abrams

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0