使用python从JSON文件中提取部分数据

时间:2016-10-18 17:11:45

标签: python json extract decode

我一直在尝试使用python从JSON文件中提取某些数据。我想从我的json创建一个数组,其中包含一些条目,如下所示

[{"Device Name":"abc", "Device Caps":["a1", "b1", "c1"]},
{"Device Name":"def", "Device Caps":["a2", "b2", "c2"]},
{"Device Name":"ghi", "Device Caps":["a3", "b3", "c3"]},
{"Device Name":"jkl", "Device Caps":["a4", "b4", "c4"]}] 

我需要输出为     [ “A1”, “A2”, “A3”, “A4”]

1 个答案:

答案 0 :(得分:1)

如果这确实是您的输入文件,那么以下代码将生成您想要的输出:

import json

with open("input.json") as input_file:
    data = json.load(input_file)
data = [d["Device Caps"][0] for d in data]

print(data)