6.2 Construct valid JSON-encoded files
Aligned to Cisco's 350-401 ENCOR v1.2 exam topics.
On this page
JSON is the language of most network APIs. Catalyst Center responses are JSON. SD-WAN Manager APIs return JSON. RESTCONF can return JSON. Python scripts often read and write JSON.
For ENCOR, you must be able to look at a JSON file and know whether it is valid. You should also understand what the structure means.
What JSON is
JSON stands for JavaScript Object Notation, but you do not need to know JavaScript to use it. Treat JSON as structured text for data exchange.
A JSON document is built from:
- objects;
- arrays;
- strings;
- numbers;
- booleans;
- null.
Top-level response object
Array of device objects
Fields such as hostname and reachableObjects
An object is a set of key-value pairs. It uses curly braces.
{
"hostname": "R1",
"mgmt_ip": "10.10.10.11",
"platform": "IOS-XE"
}Rules:
- keys must be strings;
- strings must use double quotes;
- each key is followed by a colon;
- key-value pairs are separated by commas;
- no trailing comma after the last pair.
Invalid:
{
hostname: "R1",
"mgmt_ip": "10.10.10.11",
}Why invalid?
hostnameis not in double quotes;- there is a trailing comma after the last field.
Arrays
An array is an ordered list. It uses square brackets.
[
"R1",
"R2",
"SW1"
]Arrays often contain objects:
{
"devices": [
{
"hostname": "R1",
"mgmt_ip": "10.10.10.11"
},
{
"hostname": "SW1",
"mgmt_ip": "10.10.10.12"
}
]
}Read this as: one object with a key named devices; that key contains an array of device objects.
Data types
JSON values can be different types.
{
"hostname": "R1",
"enabled": true,
"interface_count": 12,
"site": null,
"tags": ["core", "wan"],
"management": {
"protocol": "ssh",
"port": 22
}
}Important details:
true,false, andnullare lowercase;- numbers do not use quotes;
- strings do use double quotes;
- objects and arrays can be nested.
Python dictionary vs. JSON
Python and JSON look similar, but they are not identical.
Python:
{
"hostname": "R1",
"enabled": True,
"site": None
}JSON:
{
"hostname": "R1",
"enabled": true,
"site": null
}The differences matter on the exam.
- Python uses
True,False,None. - JSON uses
true,false,null.
Network inventory example
A clean inventory file might look like this:
{
"site": "HQ",
"devices": [
{
"hostname": "HQ-R1",
"role": "edge-router",
"mgmt_ip": "10.10.10.11",
"platform": "IOS-XE",
"protocols": ["ssh", "restconf"]
},
{
"hostname": "HQ-SW1",
"role": "access-switch",
"mgmt_ip": "10.10.10.12",
"platform": "IOS-XE",
"protocols": ["ssh"]
}
]
}This is easy for a script to process because the structure is predictable:
site
string
devices
array
devices * .hostname
string
devices * .protocols
arrayAPI payload example
APIs often expect JSON payloads. Here is a simple example for a template variable body:
{
"templateId": "12345678-abcd-1234-abcd-1234567890ab",
"targetInfo": [
{
"id": "device-001",
"type": "MANAGED_DEVICE_UUID",
"params": {
"hostname": "HQ-SW1",
"ntp_server": "10.10.10.5"
}
}
]
}You do not need to memorize this payload. Focus on reading the nesting:
- top-level object;
targetInfoarray;- each target has
id,type, andparams; paramsis another object.
Validate JSON from the CLI
Use Python:
python3 -m json.tool inventory.jsonIf the JSON is valid, Python prints formatted JSON. If not, it gives an error with a line and column.
Example error:
Expecting property name enclosed in double quotes: line 2 column 3That usually means a key was not quoted or a trailing comma broke the file.
Use jq if installed:
jq . inventory.jsonCommon JSON mistakes
Single quotes
Invalid:
{
'hostname': 'R1'
}Valid:
{
"hostname": "R1"
}Trailing comma
Invalid:
{
"hostname": "R1",
"mgmt_ip": "10.10.10.11",
}Valid:
{
"hostname": "R1",
"mgmt_ip": "10.10.10.11"
}Python booleans
Invalid JSON:
{
"enabled": True
}Valid JSON:
{
"enabled": true
}Comments
Invalid JSON:
{
"hostname": "R1" // core router
}JSON does not allow comments. If you need notes, store them as data:
This catches a lot of candidates because comments work in JavaScript source code, YAML, many config files, and some JSON-like tools. Strict JSON does not allow comments. It also does not allow trailing commas, single-quoted strings, unquoted keys, True, False, or None.
Exam habit: when asked whether a file is valid JSON, check punctuation before meaning. A payload can describe the right network intent and still be invalid JSON.
{
"hostname": "R1",
"note": "core router"
}Lab: build and validate a device inventory
Goal
Create a valid JSON inventory and read it with Python.
Task 1: create inventory.json
{
"site": "Branch-10",
"devices": [
{
"hostname": "BR10-R1",
"mgmt_ip": "10.10.10.1",
"platform": "IOS-XE",
"role": "router",
"restconf_enabled": true
},
{
"hostname": "BR10-SW1",
"mgmt_ip": "10.10.10.2",
"platform": "IOS-XE",
"role": "switch",
"restconf_enabled": false
}
]
}Task 2: validate it
python3 -m json.tool inventory.jsonTask 3: read it with Python
Create read_inventory.py:
import json
with open("inventory.json") as f:
inventory = json.load(f)
print(f"Site: {inventory['site']}")
for device in inventory["devices"]:
restconf = "yes" if device["restconf_enabled"] else "no"
print(f"{device['hostname']} {device['mgmt_ip']} RESTCONF={restconf}")Run it:
python3 read_inventory.pyExpected output:
Site: Branch-10
BR10-R1 10.10.10.1 RESTCONF=yes
BR10-SW1 10.10.10.2 RESTCONF=noTask 4: break it on purpose
Change true to True in the JSON file. Run the validator again.
You should see an error. That error is the point of the lab: many automation failures are not deep network problems. They are malformed payloads.
What to memorize
- Object:
{ "key": "value" }. - Array:
[ item1, item2 ]. - Strings use double quotes.
- Keys use double quotes.
true,false, andnullare lowercase.- No trailing commas.
- No comments.
- JSON data maps naturally to Python dictionaries and lists.
Exam-ready summary
When you see JSON on ENCOR, slow down and check syntax first: braces, brackets, commas, colons, double quotes, lowercase booleans, and no trailing commas. Then read the structure: object, array, nested object, nested array. If you can do that, you can interpret most API payloads and avoid the easy traps.
Sources used
- Cisco ENCOR 350-401 v1.2 exam topics: https://learningcontent.cisco.com/documents/marketing/exam-topics/350-401-ENCORE-v1.2.pdf