简体   繁体   中英

Can't Read File in Python using Json.load

I'm trying to read a .json file stored in my working directory into a jupyter notebook to eventually put it into a pandas dataframe to clean it but I'm having difficulty with the json.load function.

The data is structured:

{
"URL":"http://www.just-eat.co.uk/restaurants-cn-chinese-cardiff/menu",    
"_id":{"$oid":"55f14312c7447c3da7051b26"},
"address":"228 City Road",
"address line 2":"Cardiff",
"name":".CN Chinese",
"outcode":"CF24",
"postcode":"3JH",
"rating":5,
"type_of_food":"Chinese"
}

I've manually entered two rows of data to test it which work fine

data = [{"URL":"http://www.just-eat.co.uk/restaurants-cn-chinese-cardiff/menu","_id":{"$oid":"55f14312c7447c3da7051b26"},"address":"228 City Road","address line 2":"Cardiff","name":".CN Chinese","outcode":"CF24","postcode":"3JH","rating":5,"type_of_food":"Chinese"}, {"URL":"http://www.just-eat.co.uk/restaurants-atthai-ss9/menu","_id":{"$oid":"55f14312c7447c3da7051b27"},"address":"376 Rayleigh Road","address line 2":"Essex","name":"@ Thai","outcode":"SS9","postcode":"5PT","rating":5.5,"type_of_food":"Thai"}]
df = pd.DataFrame.from_dict(data, orient='columns')
df

URL     _id     address     address line 2  name    outcode     postcode    rating  type_of_food
0   http://www.just-eat.co.uk/restaurants-cn-chine...   {u'$oid': u'55f14312c7447c3da7051b26'}  228 City Road   Cardiff     .CN Chinese     CF24    3JH     5.0     Chinese
1   http://www.just-eat.co.uk/restaurants-atthai-s...   {u'$oid': u'55f14312c7447c3da7051b27'}  376 Rayleigh Road   Essex   @ Thai  SS9     5PT     5.5     Thai

import json

​

But when I try to read the whole file in using 2 different json.load methods I get the same error message:


import json

with open('restaurant.json') as file:
    data = json.load(file)

or

import json

filename= 'restaurant.json'
Jdata= open(filename, 'r+')
print(json.load(Jdata))

I get the following error

ValueError                                Traceback (most recent call last)
<ipython-input-47-128297096a2b> in <module>()
      3 filename= 'restaurant.json'
      4 Jdata= open(filename, 'r+')
----> 5 print(json.load(Jdata))

/home/bdtech/anaconda2/lib/python2.7/json/__init__.pyc in load(fp, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    289         parse_float=parse_float, parse_int=parse_int,
    290         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
--> 291         **kw)
    292 
    293 

/home/bdtech/anaconda2/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    337             parse_int is None and parse_float is None and
    338             parse_constant is None and object_pairs_hook is None and not kw):
--> 339         return _default_decoder.decode(s)
    340     if cls is None:
    341         cls = JSONDecoder

/home/bdtech/anaconda2/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
    365         end = _w(s, end).end()
    366         if end != len(s):
--> 367             raise ValueError(errmsg("Extra data", s, end, len(s)))
    368         return obj
    369 

ValueError: Extra data: line 2 column 1 - line 2549 column 1 (char 258 - 681632)

Thanks!

Fixed - extra data error was because it was only reading one line

data = []
with open('restaurant.json') as f:
    for line in f:
        data.append(json.loads(line))

df = pd.DataFrame.from_dict(data, orient='columns')

Reads all data in the file into a dataframe as reqiured

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM