1. What JSON is and why you meet it everywhere
JSON (JavaScript Object Notation) is plain text that describes data: names and values, lists of things, things inside things. Programs read and write it easily, and so can you. When you ask a router, a firewall or a cloud dashboard for information through an API, the answer almost always comes back as JSON.
{
"hostname": "R1",
"model": "IOL",
"managed": true,
"uptime_days": 41.5,
"location": null,
"interfaces": [
{ "name": "Ethernet0/0", "ip": "10.0.12.1", "mask": 30, "enabled": true },
{ "name": "Ethernet0/1", "ip": "10.1.0.1", "mask": 24, "enabled": false }
]
}2. The six building blocks
| JSON | Python equivalent | Notes |
|---|---|---|
object {...} | dict | Keys must be strings in double quotes |
array [...] | list | Order is kept |
| string | str | Double quotes only; \n and \" escapes allowed |
| number | int or float | No leading zeros; no 0x hex; no NaN |
true / false | True / False | Lower case in JSON |
null | None | 'no value' |
Try it
3. The rules that break files
| Mistake | Wrong | Right |
|---|---|---|
| Single quotes | {'name': 'R1'} | {"name": "R1"} |
| Unquoted key | {name: "R1"} | {"name": "R1"} |
| Trailing comma | [10, 20, 30,] | [10, 20, 30] |
| Missing comma | {"a": 1 "b": 2} | {"a": 1, "b": 2} |
| Comment | {"a": 1 // note} | JSON has no comments; remove it or put the note in a field |
| Python value | {"ok": True, "v": None} | {"ok": true, "v": null} |
| Unclosed bracket | {"a": [1, 2} | {"a": [1, 2]} |
| Number as text confusion | "vlan": "10" when 10 is expected | "vlan": 10 |
Try it
4. Reading a path into the data
| Question | In Python | In jq (command-line tool) |
|---|---|---|
| The hostname | data["hostname"] | .hostname |
| The first interface's name | data["interfaces"][0]["name"] | .interfaces[0].name |
| All interface names | [i["name"] for i in data["interfaces"]] | .interfaces[].name |
| Only enabled ones | [i for i in data["interfaces"] if i["enabled"]] | .interfaces[] | select(.enabled) |
Arrays count from 0. Asking for a key that does not exist gives an error in Python (KeyError); .get("key") returns None instead.
Try it
5. Tools for working with JSON
| Tool | Use |
|---|---|
python -m json.tool file.json | Checks the file and pretty-prints it; the error message points at the problem |
curl -s URL | python -m json.tool | Prettify an API reply in the terminal |
| VS Code, browser developer tools | Colour, folding and live error markers |
jq | A small command-line filter for JSON (install it with dnf, apt, apk or winget) |
| jsonlint, online validators | Never paste real configs or secrets into an online tool |
| Postman / Bruno | Send API requests and see the formatted JSON |
Try it
6. JSON in the real world of networking
// A trimmed reply from a cloud-managed dashboard (field names vary by vendor)
[
{ "name": "HQ-SW1", "serial": "Q2XX-AAAA", "model": "MS250-24", "networkId": "N_1234", "status": "online" },
{ "name": "HQ-AP1", "serial": "Q2YY-BBBB", "model": "MR46", "networkId": "N_1234", "status": "alerting" }
]
// RESTCONF reply for one interface on an IOS XE router (the key has a module name and a colon)
{ "ietf-interfaces:interface": { "name": "GigabitEthernet1", "type": "iana-if-type:ethernetCsmacd", "enabled": true } }Notice the first one is an array of objects (a list of devices) and the second a single object. Always ask: is the outermost thing a list or an object?
Try it
Check yourself
Think first, type an answer if you like, then reveal.