Surbhi Misra
Surbhi Misra

Reputation: 39

Error in reading JSON: No JSON object could be decoded

I am reading a set of JSON files using glob and storing them in a list. The length of the list is 1046. When I am reading the JSON file one by one and loading it to run further code, it just runs on 595 files and gives the following error:

Traceback (most recent call last):
File "removeDeleted.py", line 38, in <module>
d = json.load(open(fn))
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I am loading the json files like this:

json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json")) 
for fn in json_file_names:
    #print fn
    #temp = temp + 1
    #count = 0
    d = json.load(open(fn))
    objects = d["objects"]
    for j in range(len(objects)):

Can anybody suggest me way out of this error?

Upvotes: 0

Views: 3120

Answers (2)

Peter van Heusden
Peter van Heusden

Reputation: 99

As Blender said, you need to find out which of your files contains invalid JSON. To this end, you need to add some debugging statements to your code:

json_file_names = sorted(glob.glob("./Intel_Shared_Data/gtFine/train/*/*.json")) 
  for fn in json_file_names:
   #print fn
   #temp = temp + 1
   #count = 0
   try:
       d = json.load(open(fn))
       objects = d["objects"]
       for j in range(len(objects)):
   except ValueError as e:
       print "Could not load {}, invalid JSON".format({})

Upvotes: 1

avigil
avigil

Reputation: 2246

One of your json text files is empty. Maybe start by seeing if you have any zero size files with

find . -size 0

run from your directory of json files in a terminal.

Upvotes: 0

Related Questions