2017-11-16 9 views
-1

私は深いjsonファイルを正規化するために多くの方法(そしてstackoverflowの多くの質問で)を試みてきました。 私は.apply(pd.Series)で試したことがあります。jsonファイルの掘り下げ

私は現在json_normalizeで試しており、いくつかの結果が得られました。私は機能の仕組みを知っていると思うし、私の問題は辞書をどのようにナビゲートするのかわからないことだと思う。

これまでのところ、私は2つのレベルに掘り下げることができました。

import json 
import pandas as pd 
from pandas.io.json import json_normalize 
raw = json.load(open('authors.json')) 
raw2 = json_normalize(raw['hits']['hits']) 

これは私に必要なもの(少なくとも最初のレベル)を与えます。しかし、私はもっと深く行く方法を知らない。

私が試してみた:

raw2 = json_normalize(raw['hits']['hits'][0]) 
raw2 = json_normalize(raw['hits']['hits']['_source.authors']) 
TypeError: string indices must be integers 

そして、多くの詳細が、ちょうどランダムに理解せずに何かをしようとすることは正しい方法ではありません。

  • どのように私は次のレベル(JSONで[]{})を含める方法を知っていますか:私は私の質問があると思いますか?
  • これを表すための視覚的な方法はありますか?

このトピックはオンラインではありません。私は毎日jsonデータでより多くの仕事をしています。

_id _index _score _source.authors _source.deleted _source.description _source.doi _source.is_valid _source.issue _source.journal ... _source.rating_versatility_weighted _source.review_count _source.tag _source.title _source.userAvg _source.user_id _source.venue_name _source.views_count _source.volume _type 
0 7CB3F2AD scibase_listings 1 None 0 None  1 None Physical Review Letters ... 0 0 [mass spectra, elementary particles, bound sta... Evidence for a new meson: A quasinuclear NN-ba... 0 None Physical Review Letters 0 None listing 
1 7AF8EBC3 scibase_listings 1 [{'affiliations': ['Punjabi University'], 'aut... 0 None  1 None Journal of Industrial Microbiology & Biotechno... ... 0 0 [flow rate, operant conditioning, packed bed r... Development of a stable continuous flow immobi... 0 None Journal of Industrial Microbiology & Biotechno... 0 None listing 
2 7521A721 scibase_listings 1 [{'author_id': '7FF872BC', 'author_name': 'bar... 0 None  1 None The American Historical Review ... 0 0 [social movements] Feminism and the women's movement : dynamics o... 0 None The American Historical Review 0 None listing 

これはファイルのチャンクです(レベル3、レベル1、レベル2、ヒット、ヒット)。

{'_shards': {'failed': 0, 'successful': 5, 'total': 5}, 
'hits': {'hits': [{'_id': '7CB3F2AD', 
    '_index': 'scibase_listings', 
      "_type": "listing", 
      "_id": "7FDFEB02", 
      "_score": 1, 
      "_source": { 
       "userAvg": 0, 
       "meta_keywords": null, 
       "views_count": 0, 
       "rating_reproducability": 0, 
       "rating_versatility": 0, 
       "rating_innovation": 0, 
       "tag": null, 
       "rating_reproducibility_weighted": 0, 
       "meta_description": null, 
       "review_count": 0, 
       "rating_avg_weighted": 0, 
       "venue_name": "The American Historical Review", 
       "rating_num_weighted": 0, 
       "is_valid": 1, 
       "normalized_venue_name": "american historical review", 
       "rating_clarity": 0, 
       "description": null, 
       "deleted": 0, 
       "journal": "The American Historical Review", 
       "volume": null, 
       "link": null, 
       "authors": [ 
        { 
         "author_id": "166468F4", 
         "author_name": "a bowdoin van riper" 
        }, 
        { 
         "author_id": "81070854", 
         "author_name": "jeffrey h schwartz" 
        } 
       ], 
       "user_id": null, 
       "pub_date": "1994-01-01 00:00:00", 
       "pages": null, 
       "doi": "", 
       "issue": null, 
       "rating_versatility_weighted": 0, 
       "pubtype": null, 
       "title": "Men Among the Mammoths: Victorian Science and the Discovery of Human Prehistory", 
       "rating_clarity_weighted": 0, 
       "rating_innovation_weighted": 0 
      } 
     }, 
     { 
      "_index": "scibase_listings", 
      "_type": "listing", 
      "_id": "7538108B", 
      "_score": 1, 
      "_source": { 
       "userAvg": 0, 
       "meta_keywords": null, 
       "views_count": 0, 
       "rating_reproducability": 0, 
       "rating_versatility": 0, 
       "rating_innovation": 0, 
       "tag": null, 
       "rating_reproducibility_weighted": 0, 
       "meta_description": null, 
       "review_count": 0, 
       "rating_avg_weighted": 0, 
       "venue_name": "The American Historical Review", 
       "rating_num_weighted": 0, 
       "is_valid": 1, 
       "normalized_venue_name": "american historical review", 
       "rating_clarity": 0, 
       "description": null, 
       "deleted": 0, 
       "journal": "The American Historical Review", 
       "volume": null, 
       "link": null, 
       "authors": [ 
        { 
         "affiliations": [ 
          "Pennsylvania State University" 
         ], 
         "author_id": "7E15BDFA", 
         "author_name": "roger l geiger" 
        } 
       ], 
       "user_id": null, 
       "pub_date": "2013-06-01 00:00:00", 
       "pages": null, 
       "doi": "10.1093/ahr/118.3.896a", 
       "issue": null, 
       "rating_versatility_weighted": 0, 
       "pubtype": null, 
       "title": "Elizabeth Popp Berman. Creating the Market University: How Academic Science Became an Economic Engine.", 
       "rating_clarity_weighted": 0, 
       "rating_innovation_weighted": 0 
      } 
     } 
    ] 
+0

__valid__ JSON文字列/ファイルを指定すると解析できますか?あなたの質問からコピーして、 'json.loads(json_string) 'に渡してみてください。もう一つの役に立つリソースは:https://jsonlint.com/ - JSONファイルをオンラインで検証します – MaxU

+0

それはうまくいかないのですか?元のチャンクです。 –

答えて

0

私はjsonを「掘る」方法を考え出したと思います。次のレベルがリストか辞書かによって異なります。

私の場合、私は以下で最後まで掘り下げることができました。私はまだ完全なリスト(おそらくループ)を使用する方法を見つける必要があるので、私はすべての値を持つことができ、ただ[0]または[1]ではありません。

raw['hits']['hits'][1]['_source']['authors'][0]['affiliations'] 
関連する問題