Abdul Jaleel Abdul Jaleel - 1 month ago 6
JSON Question

Extract part of data from JSON file using python

I have been trying to extract only certain data from a JSON file using python.I wanted to create an array from my json which has some entries as below

[{"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"]}]


I need my output as
["a1","a2","a3","a4"]

Answer

If that is literally your input file, then the following code will produce the output you want:

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)