2016-11-29 2 views
0

この入れ子になったJSONオブジェクトから、"id"の値と"location"の値を解析して出力する必要があります。pythonを使用して入れ子になったjsonオブジェクトからデータを外科的に抽出

{ 
     "links": { 
      "self": "http://localhost:2510/api/v2/jobs?skills=data%20science" 
     }, 
     "data": [ 
      { 
       "id": 121, 
       "type": "job", 
       "attributes": { 
        "title": "Data Scientist", 
        "date": "2014-01-22T15:25:00.000Z", 
        "description": "Data scientists are in increasingly high demand amongst tech companies in London. Generally a combination of business acumen and technical skills are sought. Big data experience ..." 
       }, 
       "relationships": { 
        "location": { 
         "links": { 
          "self": "http://localhost:2510/api/v2/jobs/121/location" 
         }, 
         "data": { 
          "type": "location", 
          "id": 3 
         } 
        }, 
        "country": { 
         "links": { 
          "self": "http://localhost:2510/api/v2/jobs/121/country" 
         }, 
         "data": { 
          "type": "country", 
          "id": 1 
         } 
        }, 

私はこの方法でそれをつかむしようとしてきた:

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


for item in data["data"]: 
    for job in data['id']: 
     for title in data['data']: 
      print(title.get('location') 

が、私は私が必要とするデータをつかむことができていませんでした。

興味のあるデータのみを外科的に抽出するにはどうすればよいですか?

This is the full file


EDIT

私は探検のいくつかの種類として、これをしようとしてきたが、それでもこれは、ファイルが終了する前にクラッシュ:

import json 
from pprint import pprint 

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


for item in data["data"]: 
    for job in item: 
      print(job) 
+0

ええ、私はそれ以来変わってきました。 **編集の下に新しいコードを入れます** –

+1

あなたのコードをコピーしてファイルをダウンロードしてもエラーは発生しません。 –

+0

ああ - ええ - 私はそれを書いてから変更しました。まだ私は私が探しているものを得ることができません。これは、データのアイテム[data "]のようになります。アイテムの属性:属性のタイトル['title']:print(title)'実際に取得しようとしているのは、 id –

答えて

1

私は飛躍を取って推測していますあなたが本当に最後に望む情報、各場所IDのアイテムIDのリストです:

import json 
from collections import defaultdict 

with open('prettyPrint.txt') as data_file: 
    data = json.load(data_file) 

locations = defaultdict(int) 

for item in data['data']: 
    location = item['relationships']['location']['data']['id'] 
    locations[location] += 1 

print(locations) 
+0

aha-はい - そうですよ!nice-ありがとう:) –

+0

は、特定のIDではなく数を取得する方法がありますか? –

+1

@ s.matthew.englishが編集されました。 –

関連する問題