1

我想将深层嵌套的 JSON 文件转换为 CSV。JSON文件是

{“abcd 性能:输出”:{“性能”:[{“样本”:{“objectId”:“abcd-device:100:12”,“ifName”:“Carrier1/1”,“timeStamp”:“ 2021-10-20T03:15:00.000Z", "type": "radio", "invalid": false, "attribute": [ { "parameter": "rslReadingMean", "value": -36.4 }, { "参数”:“qam-32rx_time”,“值”:0 } ] } },{“样本”:{“objectId”:“abcd-device:100:12”,“ifName”:“Carrier1/1”,“ timeStamp": "2021-10-20T03:30:00.000Z", "type": "radio", "invalid": false, "attribute": [ { "parameter": "rslReadingMean", "value": -36.5 },{“参数”:“qam-32rx_time”,“值”:0}]}},{“样本”:{“objectId”:“abcd-device:100:13”,“ifName”:“终端”,“ timeStamp": "2021-10-20T03:30:00.000Z", "type": "sensor", "invalid": false, "attribute": [ { "parameter": "InputCurrent", "value": 1.14 } ,{“参数”:“温度”,“值”:61.5 },{“参数”:“输入电压”,“值”:54.777 } } } ] } }000Z”,“类型”:“传感器”,“无效”:假,“属性”:[{“参数”:“输入电流”,“值”:1.14 },{“参数”:“温度”,“值” :61.5 },{“参数”:“输入电压”,“值”:54.777 } ] } } ] } }000Z”,“类型”:“传感器”,“无效”:假,“属性”:[{“参数”:“输入电流”,“值”:1.14 },{“参数”:“温度”,“值” :61.5 },{“参数”:“输入电压”,“值”:54.777 } ] } } ] } }

代码

with open('Performance_Interface_data.json') as f:
    data = json.load(f)
df = pd.json_normalize(data['abcd-performance:output'], 'performance', max_level=4)
print(df.columns)
print(df)

所需的输出是 sample_objectId;sample_ifName;sample_timeStamp;sample_type;sample_invalid;sample_attribute_parameter;sample_attribute_value

我无法进行最后一列标准化。

4

1 回答 1

0

您需要根据 JSONmeta指定参数。record_path

df = pd.json_normalize(data['abcd-performance:output']['performance'], record_path=['sample','attribute'], meta=[['sample', 'objectId'], ['sample', 'ifName'], ['sample', 'timeStamp'], ['sample', 'type'], ['sample', 'invalid']])

In [50]: df
Out[50]: 
        parameter   value     sample.objectId sample.ifName          sample.timeStamp sample.type sample.invalid
0  rslReadingMean -36.400  abcd-device:100:12    Carrier1/1  2021-10-20T03:15:00.000Z       radio          False
1   qam-32rx_time   0.000  abcd-device:100:12    Carrier1/1  2021-10-20T03:15:00.000Z       radio          False
2  rslReadingMean -36.500  abcd-device:100:12    Carrier1/1  2021-10-20T03:30:00.000Z       radio          False
3   qam-32rx_time   0.000  abcd-device:100:12    Carrier1/1  2021-10-20T03:30:00.000Z       radio          False
4    InputCurrent   1.140  abcd-device:100:13      Terminal  2021-10-20T03:30:00.000Z      sensor          False
5     Temperature  61.500  abcd-device:100:13      Terminal  2021-10-20T03:30:00.000Z      sensor          False
6    InputVoltage  54.777  abcd-device:100:13      Terminal  2021-10-20T03:30:00.000Z      sensor          False
于 2021-10-27T05:25:53.917 回答