3

I try parsing a very simple json string I get from the net: {"price" : '10.25'} As you can see, the number (10.25) is between single quotes and it seems to be a problem for simple json:

Reproduction:

import simplejson as json
json_str = """ {"price" : '10.25'} """
json.loads(json_str)

Result:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.5/simplejson/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/pymodules/python2.5/simplejson/decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/pymodules/python2.5/simplejson/decoder.py", line 353, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

However, if I change the single quotes to double ones - it works.\ Unfortunately, the jsons I get are not as simple as in the example above, so I can't just replace all single quotes with string replace command.

Anybody knows what is the right way to parse this json?

P.S. I use python 2.5.

Thanks a lot!

3
  • 1
    This isn't valid JSON. Feb 5, 2013 at 9:51
  • 1
    you mix " and '. json specs indicate " only is to be used. ' is indeed invalid. see www.json.org (!)
    – njzk2
    Feb 5, 2013 at 9:51
  • I wonder how/why the OP is getting invalid JSON in the first place. It makes it sound like they're not under the OP's control. Is it the OP's responsibility to handle incorrect JSON? it sounds like they've been given it.
    – sotapme
    Feb 5, 2013 at 10:00

4 Answers 4

3

{"price" : "10.25"} , JSON contains double quotes only.

The JSON with single quotes is invalid ( see : www.jsonlint.com ) :

Parse error on line 2:
{    "price": '10.25'}
--------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

You can correct your json using regex replace, or use ast.literal eval to load it as python object ( or dump it as json and load it again )

>>> a = """ {"price" : '10.25'} """
>>> import ast
>>> new_dict = ast.literal_eval(a.strip())
>>> import json
>>> json.dumps(new_dict)
'{"price": "10.25"}'
3
  • @acron, sorry, my bad. Corrected that. Feb 5, 2013 at 10:00
  • @DhruvPathak: Seems to work on python 2.7, however, in my production I have python 2.5 with no ast package... :(
    – diemacht
    Feb 5, 2013 at 10:22
  • @diemacht I guess there is _ast module in Python 2.5, not sure if it works the same. Feb 5, 2013 at 11:59
0

Single quotes are not valid JSON so simplejson is doing exactly what it should be. The problem is the JSON you're receiving, unfortunately. I can't see a situation where a little regex-fu can't help you find and replace the single quotes you need to.

0

The value should not be quoted if only numeric or should double quoted. So the correct way to do it is:

import simplejson as json
json_str = """ {"price" : 10.25} """
json.loads(json_str)

And to replace it you can parse the json string with some regluar expression using e.g. finditer

1
0

Check what is a valid JSON format at json.org.

As you can see there, only double quotation is valid for a string (key of dictionary).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.