Instiq
Chapter 6 · Automation and Artificial Intelligence·v1.0.0·Updated 7/20/2026·~18 min

What's changed: Initial version

6.2Constructing JSON and YANG

Key points

Teaches reading and writing JSON to the level of judging whether a document is valid (object {} vs. array [], no trailing comma, keys in double quotes, value-type distinctions), and then YANG as the language that defines data types and structure with container/list/leaf, understood as the benefit of model-driven operations.

Once you exchange payloads with RESTCONF or a controller API, an invalid JSON body is rejected before it ever reaches the device. What this section builds is not an explanation of "what JSON is" but the ability to judge a presented fragment valid or invalid on sight and repair it character by character. And where JSON is the container for data, YANG is the type definition stating what belongs in it. Blurring the two as "both about data" produces the misconception that "JSON can enforce types"—in reality YANG guarantees type and structure, and that guarantee is what makes consistent, cross-vendor automation possible.

6.2.1Rules for a valid JSON document

  • An object uses braces {} to list key/value pairs; an array uses brackets [] to list values in order. They nest, and the standard shape is "an array inside an object, objects inside the array," as in {"interfaces": [{"name": "Gi1", "enabled": true}, {"name": "Gi2", "enabled": false}]}. A document is invalid unless the kind and count of opening and closing brackets match, and closing a { with a ] is a common break.
  • Keys must always be wrapped in double quotes ({"name": "Gi1"} is valid; {name: "Gi1"} is not). JavaScript object literals and Python dictionaries permit unquoted keys and single quotes, but JSON accepts no single quotes at all{'name': 'Gi1'} is invalid. This gap bites when a Python dict is stringified and sent as-is, which is exactly why you pass it through json.dumps().
  • No comma may follow the last element (a trailing comma is invalid). {"a": 1, "b": 2,} and [1, 2, 3,] are syntax errors, and the parser rejects the whole document. This is the most frequent break in payloads edited by adding or removing lines, and the key point is that "one character in the editor means the entire document is invalid to the API."
  • Value types are distinct: strings carry double quotes ("1500"), numbers do not (1500), booleans are lowercase true/false, and the empty value is null. True/False (Python's capitalized form) and None are invalid in JSON, and since "1500" and 1500 are different types, sending a quoted value where a number is expected can be rejected as a type error.

Continue reading — free sign-up

You're reading the free preview. Sign up free to read this section in full, plus every chapter (including 4+) and all questions.