SwitchNRoutesYAML and JSON lessonsAll interactive lessonsBlogGet a quote
YAML AND JSON | INTERACTIVE LESSON

Module 1: JSON from zero

The data format almost every network API speaks: its six rules, how to read it, and the mistakes that break it.

Why it matters: REST APIs, Meraki, Catalyst Center, RESTCONF, Ansible output and Python all hand you JSON.
All YAML and JSON lessons
Progress is saved in this browser

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.

Where JSON appearsYou or a script:GET /interfacesAPI on the deviceor in the cloudReply: JSON textYour code or your eyesread it
JSON is the language of the reply. Learn to read it fluently and APIs stop being scary.
{
  "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

That is all of JSONObject { } a set of named values: {"name": "R1", "vlan": 10}Array [ ] an ordered list: [10, 20, 30]String "text" always DOUBLE quotesNumber 42 3.14 -7 (no quotes)Boolean true false (lower case)Null null 'nothing here'
Objects and arrays can hold any of the six, including other objects and arrays. That is how you get deep nesting.
JSONPython equivalentNotes
object {...}dictKeys must be strings in double quotes
array [...]listOrder is kept
stringstrDouble quotes only; \n and \" escapes allowed
numberint or floatNo leading zeros; no 0x hex; no NaN
true / falseTrue / FalseLower case in JSON
nullNone'no value'

Try it

3. The rules that break files

MistakeWrongRight
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
RememberJSON is strict: one stray comma and the whole document is rejected. The error message gives a line and column. Look there first, then one line above.

Try it

4. Reading a path into the data

The same data as a tree root object hostname: "R1" managed: true interfaces [array] [0] object [1] object name, ip, mask, enabled
To reach the first interface's IP: root, then interfaces, then item 0, then ip.
QuestionIn PythonIn jq (command-line tool)
The hostnamedata["hostname"].hostname
The first interface's namedata["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

ToolUse
python -m json.tool file.jsonChecks the file and pretty-prints it; the error message points at the problem
curl -s URL | python -m json.toolPrettify an API reply in the terminal
VS Code, browser developer toolsColour, folding and live error markers
jqA small command-line filter for JSON (install it with dnf, apt, apk or winget)
jsonlint, online validatorsNever paste real configs or secrets into an online tool
Postman / BrunoSend 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.