我想将深层嵌套的 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
我无法进行最后一列标准化。