2017-10-08 17 views
1

I am trying to load in python the file business.json from yelp academic data available for their academic challenge, see below (https://www.yelp.com/dataset/documentation/json) My Goal is to extract all restaurant and their ID to then find the one restaurant I am interested for. Once I have this restaurant id, I want to load review.json and extract all reviews for that given restaurant. Sadly I am stuck at the initial stage of landing the .json負荷Pythonで大きなJCONファイル - エラー= JSONDecodeError:エクストラデータ

これはbusiness.jsonは、次のようになります。私は次のコードでbusiness.jsonインポートしようとする

{ 
    // string, 22 character unique string business id 
    "business_id": "tnhfDv5Il8EaGSXZGiuQGg", 

    // string, the business's name 
    "name": "Garaje", 

    // string, the neighborhood's name 
    "neighborhood": "SoMa", 

    // string, the full address of the business 
    "address": "475 3rd St", 

    // string, the city 
    "city": "San Francisco", 

    // string, 2 character state code, if applicable 
    "state": "CA", 

    // string, the postal code 
    "postal code": "94107", 

    // float, latitude 
    "latitude": 37.7817529521, 

    // float, longitude 
    "longitude": -122.39612197, 

    // float, star rating, rounded to half-stars 
    "stars": 4.5, 

    // interger, number of reviews 
    "review_count": 1198, 

    // integer, 0 or 1 for closed or open, respectively 
    "is_open": 1, 

    // object, business attributes to values. note: some attribute values might be objects 
    "attributes": { 
     "RestaurantsTakeOut": true, 
     "BusinessParking": { 
      "garage": false, 
      "street": true, 
      "validated": false, 
      "lot": false, 
      "valet": false 
     }, 
    }, 

    // an array of strings of business categories 
    "categories": [ 
     "Mexican", 
     "Burgers", 
     "Gastropubs" 
    ], 

    // an object of key day to value hours, hours are using a 24hr clock 
    "hours": { 
     "Monday": "10:00-21:00", 
     "Tuesday": "10:00-21:00", 
     "Friday": "10:00-21:00", 
     "Wednesday": "10:00-21:00", 
     "Thursday": "10:00-21:00", 
     "Sunday": "11:00-18:00", 
     "Saturday": "10:00-21:00" 
    } 
} 

import json 

jsonBus = json.loads(open('business.json').read()) 
for item in jsonBus: 
    name = item.get("Name") 
    businessID = item.get("business_id") 

私は次のエラーを取得する:

runfile('/Users/Nico/Google Drive/Python/yelp/yelp_academic.py', wdir='/Users/Nico/Google Drive/Python/yelp') 
Traceback (most recent call last): 

    File "<ipython-input-46-68ba9d6458bc>", line 1, in <module> 
    runfile('/Users/Nico/Google Drive/Python/yelp/yelp_academic.py', wdir='/Users/Nico/Google Drive/Python/yelp') 

    File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile 
    execfile(filename, namespace) 

    File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "/Users/Nico/Google Drive/Python/yelp/yelp_academic.py", line 3, in <module> 
    jsonBus = json.loads(open('business.json').read()) 

    File "/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads 
    return _default_decoder.decode(s) 

    File "/anaconda3/lib/python3.6/json/decoder.py", line 342, in decode 
    raise JSONDecodeError("Extra data", s, end) 

JSONDecodeError: Extra data 

誰もそのようなエラーが表示される理由を知っていますか?

私はさらにスマートな方法で進むことができます。

ベスト、

ニコ

答えて

1

あなたのJSONファイルは、あなたが述べたように、全く同じである場合、それらは標準の一部ではないとして、それは(別名:// string, 22 character unique string business id)コメントを持つべきではありません。私は、同じデータセットで働いているし、同様のエラーを持っていた - 私はこれがうまくいくと思うCan comments be used in JSON?

+1

これはyelpのウェブサイトからのコピー貼りです。私はそれがjsonにないと思います。 – Nico

+0

同じデータセットを使用していますが、同じエラーが発生していますOPです。私は "aka"([それは地名にあった](https://i.stack.imgur.com/N7cmi.jpg)で置き換えられたファイルの* only * '//')。それ以外の場合はJSONが正当に見えますが、そこにコメントはありません。 [ここにSublimeTextのJSONのスクリーンショット](https://i.stack.imgur.com/A2cja.jpg)があります。 OPがそれを示す方法は、そのリンクからのものです。実際にデータがファイルにどのようにレイアウトされているかはわかりません。 – BruceWayne

+0

'JSON_DecodeError(" Expecting value "、s、err.value)をNoneから起動します。\ n json.decoder.JSONDecodeError:' json_data = json.loads( 'business.json'期待値:行1列1(char 0) ' – BruceWayne

0

は、ここに関連記事を参照してください。コメント hereを実行しているようです。 json.loads()が動作しない理由を

import json 

js = [json.loads(line) for line in open('business.json')] 
for item in js: 
    name = item.get("name") 
    businessID = item.get("business_id") 

しかし、私はまだ思ったんだけど。ファイル自体は正常に見えます。

+1

' json.loads() 'はファイルではなく文字列を読み込み、そのファイルがJSONオブジェクト全体の1つであると想定します。このファイルには、代わりに各行に1つのJSONオブジェクトが含まれています。 –

+0

@ cricket_007 - 大丈夫です - 私はjsonを新しく(明らかに:P)、それを認識しませんでした。あなたのメモをありがとう! – BruceWayne

関連する問題