I'm having difficulty to get some data from my json file. I'm able to get some of the data but when I want to dig more in details I'm getting error and stuck no where.
Running the script below return an error
AttributeError: 'list' object has no attribute 'get'
I know its a list but I'm not sure how to get the rest of the object list.
Script
ifile=open('source.json', 'r')
ofile=open('extract.json', 'w')
json_decode=json.load(ifile)
myresult=[]
for item in json_decode:
mydict={}
mydict['sID']=item.get('Ls id')
my_dict['dID']=item.get('items').get('Link ID').get('Metric').get('Link Type')
mydict['type']=item.get('Type')
myresult.append(mydict)
myjson=json.dumps(myresult, ofile)
ofile.write(myjson)
ofile.close()
source Json file
[
{
"Ls age": "201",
"items": [
{
"Link ID": "1.1.1.2",
"Link Type": "StubNet",
"Metric": "1",
"Data": "255.255.255.255",
"Priority": "Medium"
},
{
"Link ID": "1.1.1.4",
"Link Type": "P-2-P",
"Metric": "1",
"Data": "192.168.100.34"
},
{
"Link ID": "192.168.100.33",
"Link Type": "StubNet",
"Metric": "1",
"Data": "255.255.255.255",
"Priority": "Medium"
},
{
"Link ID": "1.1.1.1",
"Link Type": "P-2-P",
"Metric": "1",
"Data": "192.168.100.53"
}
],
"Len": "84",
"Ls id": "1.1.1.2",
"Adv rtr": "1.1.1.2",
"Type": "Router",
"Link count": "5"
},
{
"Ls age": "1699",
"seq#": "80008d72",
"items": [
{
"Link ID": "1.1.1.1",
"Link Type": "StubNet",
"Metric": "1",
"Data": "255.255.255.255",
"Priority": "Medium"
},
{
"Link ID": "1.1.1.1",
"Link Type": "StubNet",
"Metric": "12",
"Data": "255.255.255.255",
"Priority": "Medium"
},
{
"Link ID": "1.1.1.3",
"Link Type": "P-2-P",
"Metric": "10",
"Data": "192.168.100.26"
},
{
"Link ID": "192.168.100.25",
"Link Type": "StubNet",
"Metric": "10",
"Data": "255.255.255.255",
"Priority": "Medium"
},
{
"Link ID": "1.1.1.2",
"Link Type": "P-2-P",
"Metric": "10",
"Data": "192.168.100.54"
},
{
"Link ID": "192.168.100.53",
"Link Type": "StubNet",
"Metric": "10",
"Data": "255.255.255.255",
"Priority": "Medium"
}
],
"Len": "96",
"Ls id": "1.1.1.1",
"chksum": "0x16fc",
"Adv rtr": "1.1.1.1",
"Type": "Router",
"Options": "ASBR E",
"Link count": "6"
}
]
Expected to have result as below
[
{
"type": "Router",
"sID": "1.1.1.2",
"dID": "1.1.1.2",
"LinkType":"StubNet",
"Metric":"1"
},
{
"type": "Router",
"sID": "1.1.1.2",
"dID": "1.1.1.4",
"Link Type": "P-2-P",
"Metric": "1"
},
{
"type": "Router",
"sID": "1.1.1.2",
"dID": "192.168.100.33",
"LinkType":"StubNet",
"Metric":"1"
},
{
"type": "Router",
"sID": "1.1.1.2",
"dID":"1.1.1.1",
"Link Type": "P-2-P",
"Metric": "1"
},
{
"type": "Router",
"sID": "1.1.1.1",
"dID": "1.1.1.1",
"LinkType":"StubNet",
"Metric":"1"
},
{
"type": "Router",
"sID": "1.1.1.1",
"dID":"1.1.1.1",
"Link Type": "StubNet",
"Metric": "12"
},
{
"type": "Router",
"sID": "1.1.1.1",
"dID": "1.1.1.3",
"LinkType":"P-2-P",
"Metric":"10"
}
]
Appreciate to advise how could proceed further. I have search around and try error and still no able to resolve it. Appreciate your advise and support. Thank you