6.2 Construct valid JSON-encoded files
Aligned to Cisco's 350-401 ENCOR v1.2 exam topics.
On this page
What this objective tests
This is a construct objective. The exam shows you JSON text. You must build valid JSON or find the syntax error.
JSON is the payload format of most network APIs. Catalyst Center, SD-WAN Manager, and RESTCONF all use it. A script fails on malformed JSON before it touches the network.
The six building blocks
A JSON document uses six types of values:
| Type | Example | Rule |
|---|---|---|
| Object | {"hostname": "R1"} | Key-value pairs in curly braces. Keys are strings. |
| Array | ["R1", "R2"] | Ordered items in square brackets. |
| String | "IOS-XE" | Always double quotes. Never single quotes. |
| Number | 24 or 99.5 | No quotes. |
| Boolean | true | Lowercase only. |
| Null | null | Lowercase only. Means no value. |
Object rules:
- Put every key in double quotes.
- Put a colon after every key.
- Separate pairs with commas.
- Do not put a comma after the last pair.
Nesting is allowed. An object can hold an array. An array can hold objects. Most API payloads are nested.
Common errors
This table covers the errors the exam uses most:
| Error | Invalid | Valid | Why it fails |
|---|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} | JSON forbids a comma before the closing brace. |
| Single quotes | {'a': 1} | {"a": 1} | JSON requires double quotes. |
| Unquoted key | {a: 1} | {"a": 1} | Keys must be strings. |
| Comments | {"a": 1} // note | {"a": 1} | JSON has no comment syntax. |
| Python booleans | {"ok": True} | {"ok": true} | JSON booleans are lowercase. |
| Python null | {"site": None} | {"site": null} | JSON uses null, not None. |
Exam habit: check punctuation before meaning. A payload can describe the correct network intent and still be invalid JSON.
Example: a valid device inventory
This file is complete and valid. Use it as a model:
{
"site": "Branch-10",
"devices": [
{
"hostname": "BR10-R1",
"mgmt_ip": "10.10.10.1",
"platform": "IOS-XE",
"restconf_enabled": true,
"interfaces": ["GigabitEthernet1", "GigabitEthernet2"],
"snmp_contact": null
},
{
"hostname": "BR10-SW1",
"mgmt_ip": "10.10.10.2",
"platform": "IOS-XE",
"restconf_enabled": false,
"interfaces": ["GigabitEthernet1/0/1"],
"snmp_contact": "[email protected]"
}
]
}Read the structure:
- The top level is an object with two keys:
siteanddevices. devicesholds an array of two objects.interfacesholds an array of strings.snmp_contactshows bothnulland a string. Both are valid.
Validate any file from the command line:
python3 -m json.tool inventory.jsonValid input prints formatted JSON. Invalid input prints an error with a line and column number.
Lab: fix the broken JSON
This lab gives you a broken file. Fix it. Then compare with the corrected version.
Start with this file named broken.json:
{
'site': 'HQ',
"devices": [
{
"hostname": "HQ-R1",
"mgmt_ip": "10.10.10.11",
"restconf_enabled": True,
},
{
"hostname": "HQ-SW1", // access switch
"mgmt_ip": "10.10.10.12"
}
],
}Do these steps:
- Run
python3 -m json.tool broken.json. - Read the first error. Fix that line.
- Run the command again. Fix the next error.
- Repeat until the command prints formatted JSON.
- Compare your file with the fixed version below.
Fixed version:
{
"site": "HQ",
"devices": [
{
"hostname": "HQ-R1",
"mgmt_ip": "10.10.10.11",
"restconf_enabled": true
},
{
"hostname": "HQ-SW1",
"mgmt_ip": "10.10.10.12"
}
]
}The broken file had five errors:
- Line 2: single quotes around
'site'and'HQ'. Changed to double quotes. - Line 7:
Trueis a Python boolean. Changed to lowercasetrue. - Line 7: trailing comma after the last pair in the object. Removed the comma.
- Line 10: a
//comment. JSON forbids comments. Removed the comment. - Line 13: trailing comma after the last element of the
devicesarray. Removed the comma.
Note: Fix errors one at a time. The parser stops at the first error. Re-run the validator after every fix.
Exam traps
- JSON strings and keys need double quotes. Single quotes fail.
- JSON forbids trailing commas and comments. JavaScript allows them; JSON does not.
true,false, andnullare lowercase.True,False, andNoneare Python.- Numbers have no quotes.
"24"is a string, not a number. - A missing comma between pairs is as invalid as an extra comma.
Pass check
You are ready when you can do these things:
- Build a nested device inventory from scratch without a syntax error.
- Find a trailing comma, a single quote, or a comment by eye.
- Explain the difference between JSON
nulland PythonNone. - Validate a file with
python3 -m json.tooland read the error location.
Sources used
- Cisco ENCOR 350-401 v1.2 exam topics: https://learningcontent.cisco.com/documents/marketing/exam-topics/350-401-ENCORE-v1.2.pdf
Related objectives
- 6.1 Interpret basic Python components and scripts
- 6.3 Describe the high-level principles and benefits of a data modeling language, such as YANG
- 6.4 Describe APIs for Cisco Catalyst Center and SD-WAN Manager
- 6.5 Interpret REST API response codes and results in payload using Cisco Catalyst Center and RESTCONF