2

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?

2
  • What does print(json_data) look like? Commented Jan 25, 2020 at 20:44
  • And what is type(json_data)? Commented Jan 25, 2020 at 20:59

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.