In the network automation realm, JSON is everywhere. Whether it’s for REST APIs or telemetry data, mastering Python’s json
module is a must-have skill for every CCNP-level engineer.
Let’s dive into the sample code from your image.
The Original Python Code (Almost Complete!)
import json
data = {
"measurement": "cpmCPUTotal1minRev",
"collectionInterval": "default",
"tagCount": "0",
"policy": None,
"devices": [{"model": "Cisco 3500 Series WLC", "ipv4": "10.10.20.52"}]
}
obj = json.dumps(data, separators=(',', ':'))
print(obj)
Explanation of Key Parts
json.dumps()
: Converts the Python dictionary (data
) into a JSON-formatted string.separators=(',', ':')
: This removes unnecessary whitespace to compact the JSON output. It’s a good trick when you’re working with APIs or need efficient data transfer.data
: A dictionary holding telemetry-style information, perfect for something like Cisco DNA Center or Cisco WLC telemetry.None
: This gets translated intonull
in JSON, keeping the output compatible with standard JSON consumers.
Why Is This Useful in Networking?
When you’re dealing with APIs or tools like Cisco DNA Center, you’re often required to send or receive data in JSON. Being able to format and parse JSON properly with Python gives you automation superpowers.
Quick Tips
- Always validate your JSON format when using
dumps()
with separators. - Use
loads()
to convert a JSON string back into a Python object. - Compact JSON (with separators) is useful for bandwidth-sensitive applications.
Cisco Relevance
In environments where you’re monitoring CPU usage, like in the above "measurement": "cpmCPUTotal1minRev"
, this JSON format could be part of a REST API payload used to report or query data from Cisco devices.