6.7 Recognize the building blocks of JSON data

CCNA 200-301 v1.1 · 6.0 Automation and Programmability

JSON is the data format of REST APIs and of most automation tools. It is plain text built from just two structures, objects and arrays, holding strings, numbers, booleans and null. For the CCNA you read a JSON document, find values in it, and spot invalid JSON.

What you must know

Objects, arrays and values

{
  "hostname": "SW1",
  "managementIp": "10.99.0.11",
  "reachable": true,
  "uptimeDays": 41,
  "location": null,
  "interfaces": [
    {"name": "GigabitEthernet0/1", "vlan": 10, "enabled": true},
    {"name": "GigabitEthernet0/2", "vlan": 20, "enabled": false}
  ]
}
TypeExampleRule
String"SW1"Double quotes only
Number41, 3.5No quotes
Booleantrue, falseLower case, no quotes
NullnullNo value
Object{"key": value}Keys are strings in double quotes
Array[1, 2, 3]Ordered list, any types

To read the VLAN of the second interface, follow the path interfaces → index 1 (arrays count from 0) → vlan: the answer is 20. In Python that is data["interfaces"][1]["vlan"].

Where JSON travels

The same format carries the change to the device (1) and the data back from it (2). Whitespace and line breaks do not matter; the structure does.

InvalidWhyValid
{'vlan': 10}Single quotes{"vlan": 10}
[1, 2, 3,]Trailing comma[1, 2, 3]
{vlan: 10}Unquoted key{"vlan": 10}
{"up": True}Booleans are lower case{"up": true}

Commands

python3 -m json.tool file.jsonValidates and pretty-prints a JSON file.
show interfaces | json (NX-OS) / RESTCONF GETWays devices return JSON.

Exam traps

Check yourself

Is {"vlans": [10, 20,]} valid JSON?
No: the trailing comma after 20.
In the example, what is interfaces[0].enabled?
true.
Which structure is ordered: an object or an array?
An array.

More topics in 6.0 Automation and Programmability