这是我的json
{
"fInstructions": [
{
"id": 155,
"type":"finstruction",
"ref": "/spm/finstruction/155",
"iLineItem":[
{
"id": 156,
"type":"ilineitem",
"ref": "/spm/ilineitem/156",
"creationDate": "2018-03-09",
"dueDate":"2018-02-01",
"effectiveDate":"2018-03-09",
"frequency":"01",
"coveredPeriodFrom":"2018-02-28",
"coveredPeriodTo":"2018-02-28",
"statusCode":"PRO",
"amount": 6
},
{
"id": 157,
"type":"ilineitem",
"ref": "/spm/ilineitem/157",
"creationDate": "2018-03-09",
"dueDate":"2018-02-01",
"effectiveDate":"2018-03-09",
"frequency":"01",
"coveredPeriodFrom":"2018-03-01",
"coveredPeriodTo":"2018-03-31",
"statusCode":"PRO",
"amount": 192
}
]
}
]
}
如果我这样做:
json_normalize(data['fInstructions'], record_path=['iLineItem'])
我按预期得到两行所有 ILI。 但是,我还想在结果集中键入父属性 id。 为此,我尝试:
json_normalize(df_data_1['fInstructions'], record_path=['iLineItem'], meta=['id', 'type'])
但后来我得到: 值错误:元数据名称 ID 冲突,需要区分前缀
所以我尝试:
json_normalize(df_data_1['fInstructions'], record_path=['iLineItem'], meta=['fInstructions.id'])
这给了我:
KeyError: "Try running with errors='ignore' as key 'fInstructions.id' is not always present"
答案是:
json_normalize(df_data_1['fInstructions'], record_path=['iLineItem'], meta='id', record_prefix='ils.')