JSON and XML — How Devices Talk Data
15 min In ProgressLearning Objectives
- Read and write basic JSON in Python
- Parse a simple XML string
- Know when you'll see each format in the real world
JSON: Lighter, Modern, Everywhere
Most modern REST APIs (including Cisco DNA Center) speak JSON -- lightweight, human-readable, maps directly onto Python dicts and lists. XML is older and heavier but still shows up in legacy systems and NETCONF.
import json
data = {
"hostname": "router1",
"interfaces": [
{"name": "Gig0/0", "status": "up"},
{"name": "Gig0/1", "status": "down"},
],
}
json_string = json.dumps(data, indent=2)
print(json_string)
parsed = json.loads(json_string)
print(parsed["interfaces"][0]["status"])
XML — Still Around in NETCONF
Same data, XML-style, parsed with the standard library.
import xml.etree.ElementTree as ET
xml_string = """<device>
<hostname>router1</hostname>
<interface name="Gig0/0">up</interface>
</device>"""
root = ET.fromstring(xml_string)
print(root.find("hostname").text)
print(root.find("interface").attrib["name"])
📝 Exercise: Convert and Filter Device Inventory
Given a JSON string of multiple devices, parse it, filter to only devices with status 'down', and print a clean report line for each one. Then do the same for the XML version below using ElementTree.
code.py
Output