'Parsing JSON data in a good way [closed]
I have some JSON
structured outputs that I would like to parse and get some values from it. But my attempts are not good and viable in deeper nested JSON
. What is a good way to get keys and values from the JSON
example below?
Let's say I want to have below data for each ISIS
neighbor:
0192.0168.0001,state,hostname,ipv4Address
0192.0168.0002,state,hostname,ipv4Address
{
"vrfs": {
"default": {
"isisInstances": {
"XXX": {
"neighbors": {
"0192.0168.0001": {
"adjacencies": [
{
"state": "up",
"circuitId": "01",
"routerIdV4": "0.0.0.0",
"interfaceName": "Port-Channel1",
"lastHelloTime": 1651827049,
"level": "level-2",
"snpa": "P2P",
"hostname": "paris",
"details": {
"stateChanged": 1651599426,
"grSupported": "Supported",
"interfaceAddressFamily": "ipv4",
"srEnabled": false,
"advertisedHoldTime": 27,
"ip4Address": "192.168.0.1",
"neighborAddressFamily": "ipv4",
"areaIds": [
"00.0000"
],
"bfdIpv4State": "adminDown",
"bfdIpv6State": "adminDown",
"grState": ""
}
}
]
},
"0192.0168.0002": {
"adjacencies": [
{
"state": "up",
"circuitId": "00",
"routerIdV4": "0.0.0.0",
"interfaceName": "Port-Channel2",
"lastHelloTime": 1651827050,
"level": "level-2",
"snpa": "P2P",
"hostname": "london",
"details": {
"stateChanged": 1651599433,
"grSupported": "Supported",
"interfaceAddressFamily": "ipv4",
"srEnabled": false,
"advertisedHoldTime": 30,
"ip4Address": "192.168.0.2",
"neighborAddressFamily": "ipv4",
"areaIds": [
"00.0000"
],
"bfdIpv4State": "adminDown",
"bfdIpv6State": "adminDown",
"grState": ""
}
}
]
}
}
}
}
}
}
}
Solution 1:[1]
The following code snippet will give you the output you want, but next time you are encouraged to try for yourself first and reach us for help about the code you tried.
import json
d = json.loads('''
{
"vrfs": {
"default": {
"isisInstances": {
"XXX": {
"neighbors": {
"0192.0168.0001": {
"adjacencies": [
{
"state": "up",
"circuitId": "01",
"routerIdV4": "0.0.0.0",
"interfaceName": "Port-Channel1",
"lastHelloTime": 1651827049,
"level": "level-2",
"snpa": "P2P",
"hostname": "paris",
"details": {
"stateChanged": 1651599426,
"grSupported": "Supported",
"interfaceAddressFamily": "ipv4",
"srEnabled": false,
"advertisedHoldTime": 27,
"ip4Address": "192.168.0.1",
"neighborAddressFamily": "ipv4",
"areaIds": [
"00.0000"
],
"bfdIpv4State": "adminDown",
"bfdIpv6State": "adminDown",
"grState": ""
}
}
]
},
"0192.0168.0002": {
"adjacencies": [
{
"state": "up",
"circuitId": "00",
"routerIdV4": "0.0.0.0",
"interfaceName": "Port-Channel2",
"lastHelloTime": 1651827050,
"level": "level-2",
"snpa": "P2P",
"hostname": "london",
"details": {
"stateChanged": 1651599433,
"grSupported": "Supported",
"interfaceAddressFamily": "ipv4",
"srEnabled": false,
"advertisedHoldTime": 30,
"ip4Address": "192.168.0.2",
"neighborAddressFamily": "ipv4",
"areaIds": [
"00.0000"
],
"bfdIpv4State": "adminDown",
"bfdIpv6State": "adminDown",
"grState": ""
}
}
]
}
}
}
}
}
}
}
''')
neighbors = d['vrfs']['default']['isisInstances']['XXX']['neighbors']
for neighbor in neighbors:
adjacencies = neighbors[neighbor]['adjacencies']
for adjacency in adjacencies:
print(f"{neighbor},{adjacency['state']},{adjacency['hostname']},{adjacency['details']['ip4Address']}")
The above code will result in:
0192.0168.0001,up,paris,192.168.0.1
0192.0168.0002,up,london,192.168.0.2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |