首页 > 解决方案 > Pandas 从嵌套的 json 中读取一个参数

问题描述

我有一个以下 json 文件,我想读取所有参数:仅“dataRecordId”并将它们存储到 df 中:

{'responseInformation': '20 metadata records in response.',
 'metaDataResponse': [{'timestampFrom': '2020-10-07T10:19:07.7810000Z',
   'timestampTo': '2020-10-07T23:59:59.9999990Z',
   'component': {'type': '', 'id': '', 'name': '', 'comment': ''},
   'resource': {'type': 'EQU', 'id': '6100380', 'name': '', 'comment': ''},
   'processStep': {'type': '', 'id': '', 'name': '', 'comment': ''},
   'context': '',
   'dataRecords': [{'dataRecordId': '171533103',
     'groupName': 'Process',
     'sensorName': 'AutomaticProcessActive',
     'profile': 'sd',
     'type': 'Switch2Way',
     'unit': 'state',
     'returnType': 'timeSeries'}]},
  {'timestampFrom': '2020-10-08T00:00:00.6540000Z',
   'timestampTo': '2020-10-08T23:59:59.9999990Z',
   'component': {'type': '', 'id': '', 'name': '', 'comment': ''},
   'resource': {'type': 'EQU', 'id': '6100380', 'name': '', 'comment': ''},
   'processStep': {'type': '', 'id': '', 'name': '', 'comment': ''},
   'context': '',
   'dataRecords': [{'dataRecordId': '171534669',
     'groupName': 'Process',
     'sensorName': 'AutomaticProcessActive',
     'profile': 'sd',
     'type': 'Switch2Way',
     'unit': 'state',
     'returnType': 'timeSeries'}]},

这是我到目前为止所做的,但我不知道如何深入结构,以实现“dataRecordId”:

import json

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

    for p in data['metaDataResponse']:
        print('p['dataRecords'])

标签: jsonpandasdataframenested

解决方案


推荐阅读