JSON



To JSON and Back Again with json



• JSON and Python go together like peanut butter and jelly
• JSON Objects convert to Dictionaries
• JSON Arrays convert to Lists

>>> import json
>>> from pprint import pprint

>>> json_example = open("json_example.json").read()
>>> pprint(json_example)

# Convert JSON strings to Dictionaries / Lists
>>> json_python = json.loads(json_example)

>>> int_name = json.python["ietf-interfaces:interface"]["name"]
>>> int_name

# Convert Dictionaries to JSON
>>> json.dumps(json_python)


# json_example.json

{
    "ietf-interfaces:interface": {
        "name": "GigabitEthernet2",
        "description": "Wide Area Network",
        "enabled": true,
        "ietf-ip:ipv4": {
            "address": [
                {
                    "ip": "172.16.0.2",
                    "netmask": "255.255.255.0"
                }
            ]
        }
    }
}

Index