6.2 Construct valid JSON files
ENCOR 350-401 v1.2 · 6.0 Automation and Artificial Intelligence
ENCOR goes one step beyond reading JSON: you must construct valid JSON files, such as a RESTCONF payload or an Ansible variable file in JSON. That means getting the structure, the types and the punctuation exactly right, and matching what the API's model expects.
What you must know
- Build objects and arrays with the right nesting for a given model.
- Use the right types: strings in double quotes, numbers and booleans unquoted, null.
- Avoid the errors that break parsers: trailing commas, single quotes, comments, unescaped characters.
- Validate a file before sending it.
Constructing a payload
A RESTCONF PATCH that enables an interface with a description and an IPv4 address follows the ietf-interfaces and ietf-ip YANG models. The keys come from the model, including module prefixes.
{
"ietf-interfaces:interface": {
"name": "GigabitEthernet2",
"description": "Uplink to DSW1",
"type": "iana-if-type:ethernetCsmacd",
"enabled": true,
"ietf-ip:ipv4": {
"address": [
{"ip": "10.0.12.1", "netmask": "255.255.255.252"}
]
}
}
}A valid body is applied and the router answers 204 (1); a single misplaced comma returns 400 Bad Request with a parser error.
Rules and checks
| Rule | Wrong | Right |
|---|---|---|
| Double quotes only | {'name': 'Gi2'} | {"name": "Gi2"} |
| No trailing comma | ["a", "b",] | ["a", "b"] |
| Booleans and null lower case, unquoted | "enabled": "True" | "enabled": true |
| Numbers unquoted when the model wants a number | "vlan-id": "10" | "vlan-id": 10 |
| Escape quotes and backslashes in strings | "desc": "5" rack" | "desc": "5\" rack" |
| No comments | // uplink | (remove it) |
Validate before sending: python3 -m json.tool payload.json prints the file formatted or the exact line of the error; editors such as VS Code underline mistakes.
Commands
python3 -m json.tool payload.json | Validate and pretty-print JSON. |
curl -X PATCH -d @payload.json -H 'Content-Type: application/yang-data+json' … | Send the file as a RESTCONF body. |
Exam traps
- The top level is one object (or one array).
- Keys are always strings in double quotes.
- YANG keys often carry a module prefix:
ietf-interfaces:interface. - A number in quotes is a string and may be rejected by the model.
Check yourself
- Fix:
{"vlans": [10, 20,], 'name': "core"}. {"vlans": [10, 20], "name": "core"}.- How do you put a double quote inside a JSON string?
- Escape it with a backslash:
\". - How do you check a JSON file before sending it?
python3 -m json.tool file.json(or any JSON linter).
Labs
Try it live: Automate the router: RESTCONF, NETCONF, JSON and EEM