pkm
pkm

Reputation: 2783

Issue in reading JSON file in python

>>> import json
>>> d2 = json.loads(open("t.json").read())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
    raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
[ RHEL - ~/testing ]$ cat t.json
{"us": u"OFF", "val": u"5"}

Here is what I have in my JSON file and when I try to read it using open and json.load and json.loads it fails.

After using json.load

>>> import json
>>> d2 = json.load(open("t.json"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
    parse_constant=parse_constant, **kw)
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
    raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
>>>

Upvotes: 2

Views: 2190

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1121924

You are using the wrong function. Use json.load() (no s!) to load data from an open file object:

d2 = json.load(open("t.json"))

The json.loads() function expects you to pass in a string, not a file object. You'd have to read your file in that case, returning the read data:

d2 = json.loads(open("t.json").read())

Next, you have invalid JSON in that file:

{"us": u"OFF", "val": u"5"}
#      ^              ^

JSON is not Python; those u prefixes are not supported nor needed. You'll need to remove those from the file before it'll load.

If you have an API producing that format, it is not giving you JSON. It could be that it is producing a (strange form of) Python syntax instead; Python itself would produce {'us': u'OFF', 'val': u'5'} (single quotes). You can have Python interpret that as Python literals with ast.literal_eval():

import ast

with open('t.json') as fileobj:
    d2 = ast.literal_eval(fileobj.read())

but it could be that the format is broken in other ways we cannot determine from a single isolated sample. It could be using true and false for boolean values, like in JSON, for example.

Better to have the API fixed rather that try and work around this broken-ness.

Upvotes: 2

Paco
Paco

Reputation: 4698

Your issue is that the JSON is not valid.

It looks like it is a python dictionnary. u'string' is a python 2 unicode string.

If you remove the u from your strings, it works fine.

>>> import json
>>> json.load(open('i.json'))
{u'val': u'5', u'us': u'OFF'}

Here is the json file:

$ cat i.json
{"us": "OFF", "val": "5"}

Upvotes: 0

backtrack
backtrack

Reputation: 8144

You are using the json.loads method. More documentation here. This method is used for string arguments only. Luckily, there is a similarly named json.load method documented here. This one can be used directly on a file object.

d2 = json.load(open("t.json"))

Upvotes: 0

Related Questions