Abdul Jaleel Abdul Jaleel - 1 year ago 111
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

Answer Source

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]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download