Reputation: 45
I'm retrieving JSON data through NAPALM, it outputs quite alot of data and I want to be able to print only a few elements from it. My current code is;
from napalm import get_network_driver
import json
import paramiko
driver = get_network_driver('nxos_ssh')
LD9AGGSW01 = driver('10.249.9.44', 'username', 'password',)
LD9AGGSW01.open()
json_data = LD9AGGSW01.get_facts()
print(json.dumps(json_data, indent=4))
and the JSON data i get back is;
{
"uptime": 58404121,
"vendor": "Cisco",
"os_version": "7.0(3)I3(1)",
"serial_number": "FDO211410B8",
"model": "Nexus9000 C92160YC-X chassis",
"hostname": "Nexus-Switch-01",
"fqdn": "",
"interface_list": [
"mgmt0",
"Ethernet1/1",
"Ethernet1/2",
]
}
How would I pass through things such as the "model","hostname data" and "mgmt0" only to a print function?
Upvotes: 2
Views: 88
Reputation: 22942
If you want to print a partial dictionary, you can try:
print(
{
"model": json_data["model"],
"hostname": json_data["hostname"],
"first_interface": json_data["interface_list"][0],
}
)
You can also implement an extraction function, like this:
def extract(data):
return {
"model": data["model"],
"hostname": data["hostname"],
"first_interface": data["interface_list"][0],
}
print(extract(json_data))
Upvotes: 2