猿问

带有列表的json_normalize JSON文件包含字典(包括示例)

这是我正在处理的2条记录的示例json文件:


[{"Time":"2016-01-10",

"ID"

:13567,

"Content":{

    "Event":"UPDATE",

    "Id":{"EventID":"ABCDEFG"},

    "Story":[{

        "@ContentCat":"News",

        "Body":"Related Meeting Memo: Engagement with target firm for potential M&A.  Please be on call this weekend for news updates.",

        "BodyTextType":"PLAIN_TEXT",

        "DerivedId":{"Entity":[{"Id":"Amy","Score":70}, {"Id":"Jon","Score":70}]},

        "DerivedTopics":{"Topics":[

                            {"Id":"Meeting","Score":70},

                            {"Id":"Performance","Score":70},

                            {"Id":"Engagement","Score":100},

                            {"Id":"Salary","Score":70},

                            {"Id":"Career","Score":100}]

                        },

        "HotLevel":0,

        "LanguageString":"ENGLISH",

        "Metadata":{"ClassNum":50,

                    "Headline":"Attn: Weekend",

                    "WireId":2035,

                    "WireName":"IIS"},

        "Version":"Original"}

                ]},

"yyyymmdd":"20160110",

"month":201601},

{"Time":"2016-01-12",

"ID":13568,

"Content":{

    "Event":"DEAL",

    "Id":{"EventID":"ABCDEFG2"},

    "Story":[{

        "@ContentCat":"Details",

我试图得到一个数据帧在实体ID的水平(提取Amy,并Jon从记录1和Bob,Jon,Jack从记录2)。


但是我早早就遇到了错误。到目前为止,这是我的代码,假设示例json被另存为sample.json:


data = json.load(open('sample.json'))

test = json_normalize(data, record_path=['Content', 'Story'])

导致此错误:


TypeError: string indices must be integers

我怀疑这是因为Content.Story实际上是一个包含字典的列表,而不是字典本身。但是我不清楚如何真正克服这个问题?


编辑:为澄清起见,我最终试图达到实体ID的级别(内容>故事> DerivedID>实体> ID)。在显示Content.Story代码示例只是为了说明我现在在弄清楚这一点的位置。


偶然的你
浏览 387回答 1
1回答

宝慕林4294392

json_normalize(data, record_path=[['Content', 'Story']])那应该工作。
随时随地看视频慕课网APP

相关分类

Python
我要回答