Skip to content Skip to sidebar Skip to footer

Json.loads() Giving Exception That It Expects A Value, Looks Like Value Is There

Code: loaded_json = json.loads(json_set) json_set is a String gleaned from a webpage which is JSON formatted data. The full String (warning: LONG) is here: http://pastebin.com/wyk

Solution 1:

If you look at the ECMA-404 standard for JSON, you'll see that numbers may have an optional leading minus sign which they designate as U+002D which is the ASCII minus sign. However, \u002D is not a minus sign. It is a character escape for a minus sign, but character escapes are only valid in the context of a string value. But string values must start and end with double quotes, so this is not a string value. Thus the data you have does not parse as a valid JSON value, and the Python JSON parser is correct in rejecting it.

If you try validating that data blob using the http://jsonlint.com/ website, it will also report that the data is not valid JSON.

Parse error on line 2172:
...        "distance": \u002d1,           
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

The example you give with IDLE working is not an equal comparison because the string you gave is different:

'"distance":\u002d1' != '"distance":\\u002d1'

The string on the left is the string you gave IDLE and if you enclosed it in curly braces, it would be valid JSON:

>>> json.loads('{"distance":\u002d1}')
{'distance': -1}

But if you give it the string on the right, you'll see that it will not work as you expect:

>>> json.loads('{"distance":\\u002d1}')
Traceback (most recent calllast):
  File "/usr/lib/python3.2/json/decoder.py", line 367, in raw_decode
    obj, end= self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent calllast):
  File "<stdin>", line 1, in<module>
  File "/usr/lib/python3.2/json/__init__.py", line 309, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.2/json/decoder.py", line 351, in decode
    obj, end= self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.2/json/decoder.py", line 369, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Post a Comment for "Json.loads() Giving Exception That It Expects A Value, Looks Like Value Is There"