Rajesh Kumar
Rajesh Kumar

Reputation: 1290

Why single and double quotation marks are different in python json

I know that both single and double quotes will work same way in python, but why it is behaving differently in following two cases

>>> import json
>>> json.loads('{"M":2}')
{u'M': 2}

And in

>>> json.loads("{'M':2}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\p\python\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "c:\p\python\lib\json\decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\p\python\lib\json\decoder.py", line 376, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 1 (char 1)

Thanks in advance

Upvotes: 0

Views: 128

Answers (1)

Paco
Paco

Reputation: 4698

>>> json.loads('{"M":2}')
{u'M': 2}

that returns a python dictionary

JSON only accepts double quotes.

json.loads("{'M':2}")

This is not a valid JSON.

See: jQuery.parseJSON single quote vs double quote

Upvotes: 1

Related Questions