THEORY · TH-REST-002
Topic 2.7 of the AUTOCOR asks you to construct a YAML or JSON representation of a network configuration given a YANG-based data model. The model alone does not tell you how to write the data. That is decided by encoding rules: RFC 7950 for XML and RFC 7951 for JSON. YAML has no RFC of its own, so it reuses the JSON tree. This page turns those rules into a method you can apply to any model. Every example was validated against the IOS XE 26.2.1 models.
BLUEPRINT · AUTOCOR 350-901 v2.0: 2.7 · CCIE Automation v1.2: 2.5
Jump to: One tree · A method to build JSON from a tree · JSON rules that change the payload (RFC 7951) · The same data in XML · The body depends on the RESTCONF target · YAML · Validate before you send · What the exam expects · Exam check · References
Prerequisite: TH-REST-001 (reading a YANG model and a pyang tree).
What you will be able to do after this page
- Build a valid JSON instance from a pyang tree, step by step.
- Apply every RFC 7951 rule that changes the JSON: module-qualified names, lists, leaf-lists,
empty, 64-bit numbers,identityref. - Write the same data in XML, with the correct namespaces.
- Write it in YAML without the type traps of YAML parsers.
- Shape the body for the RESTCONF target you are sending to.
- Validate an instance before sending it.
SECTION 1
One tree, three encodings
A YANG instance is a tree of data nodes. JSON, XML and YAML are three ways of writing the same tree:
| JSON (RFC 7951) | XML (RFC 7950) | YAML | |
|---|---|---|---|
| Who defines the rules | IETF, RFC 7951 | IETF, RFC 7950 | No standard for YANG data; the JSON tree is used |
| Module identification | Module name in the member name (Cisco-IOS-XE-vlan:vlan-list) | XML namespace (xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan") | Same as JSON |
| RESTCONF media type | application/yang-data+json | application/yang-data+xml | Not sent on the wire: converted to JSON first |
| Where you meet it | RESTCONF, gNMI, Ansible, Python | NETCONF (always), RESTCONF | Ansible variables, source of truth files |
RFC 8040 requires a RESTCONF server to support XML or JSON, and allows both. IOS XE supports both. NETCONF only uses XML.
SECTION 2
A method to build JSON from a tree
Apply these six steps to any model:
- Pick the target. Decide which node the data starts at: the whole
nativetree, one container, or one list entry. The target decides the first member (section 5). - Write the first member with the module name.
"Cisco-IOS-XE-native:native". - Walk down the tree and map each node type:
| Tree | JSON |
|---|---|
+--rw name container | "name": { ... } |
+--rw name* [key] list | "name": [ { "key": ..., ... } ], always an array, even with one entry |
+--rw name* type leaf-list | "name": [ value, value ] |
+--rw name? type leaf | "name": value |
(choice) / :(case) | Nothing: write only the leafs of the chosen case |
- Add the module name when the module changes. A node augmented from another module (the
prefix:in the pyang tree) is written as"module-name:node". Its children go without a prefix, until the module changes again. - Encode each leaf by its type (section 3).
- Validate before sending (section 7).
Here are steps 1 to 5 applied to VLAN 10 named USERS, using this tree:
module: Cisco-IOS-XE-native
+--rw native
+--rw vlan
+--rw ios-vlan:vlan-list* [id]
+--rw ios-vlan:id uint16
+--rw ios-vlan:name? string
{
"Cisco-IOS-XE-native:native": {
"vlan": {
"Cisco-IOS-XE-vlan:vlan-list": [
{ "id": 10, "name": "USERS" }
]
}
}
}
native is the first member, so it carries its module. vlan belongs to the same module, so it goes without one. vlan-list comes from another module (ios-vlan: in the tree), so it carries Cisco-IOS-XE-vlan. id and name belong to that same module, so they go without one. vlan-list is a list, so its value is an array.
SECTION 3
JSON rules that change the payload (RFC 7951)
Names. RFC 7951 requires the namespace-qualified form (module:name) on every member of the top-level JSON object. Everywhere else the simple name is used. The module name only reappears when a node’s module differs from its parent’s.
Node types:
- container → JSON object;
- list → array of objects; the order of members inside each object does not matter;
- leaf-list → array of values;
- leaf → a value whose JSON type depends on the YANG type.
Leaf types. This is where most rejected payloads come from:
| YANG type | JSON | IOS XE example |
|---|---|---|
int8/16/32, uint8/16/32 | number | "id": 10 (VLAN, uint16) |
int64, uint64, decimal64 | string | "sequence": "10" (ACL, uint64) |
string | string, even if it looks like a number | "name": "2" (native interface) |
boolean | true / false, without quotes | "enabled": true (ietf-interfaces) |
empty | [null] | "shutdown": [null] |
enumeration | string with the enum name | "action": "deny" |
identityref | string; must be module-qualified if the identity is in another module | "type": "iana-if-type:softwareLoopback" |
bits | string with the names of the set bits, separated by spaces | — |
union | the value of one of the member types; the JSON type helps pick which | "protocol": "tcp" (ACL) |
Why 64-bit numbers become strings. RFC 7951 keeps the JSON within I-JSON. Common JSON parsers store numbers as 64-bit floating point, which cannot represent every 64-bit integer exactly. Sending them as strings avoids silent rounding.
A complete, validated instance. This payload configures a hostname, two VLANs, one interface, one ACL with a remark (leaf-list) and OSPF process 1. It was validated against the IOS XE 26.2.1 models:
{
"Cisco-IOS-XE-native:native": {
"hostname": "R1",
"vlan": {
"Cisco-IOS-XE-vlan:vlan-list": [
{ "id": 10, "name": "USERS" },
{ "id": 20, "name": "VOICE" }
]
},
"interface": {
"GigabitEthernet": [
{ "name": "2", "description": "Uplink to R2", "shutdown": [null] }
]
},
"ip": {
"access-list": {
"Cisco-IOS-XE-acl:extended": [
{
"name": "BLOCK-TELNET",
"access-list-seq-rule": [
{
"sequence": "10",
"remarks": ["Block Telnet from anywhere"],
"ace-rule": {
"action": "deny",
"protocol": "tcp",
"any": [null],
"dst-any": [null],
"dst-eq": "telnet"
}
}
]
}
]
}
},
"router": {
"Cisco-IOS-XE-ospf:router-ospf": {
"ospf": {
"process-id": [
{ "id": 1, "router-id": "1.1.1.1" }
]
}
}
}
}
}
Count the module names: native (first member), vlan-list (from Cisco-IOS-XE-vlan), extended (from Cisco-IOS-XE-acl) and router-ospf (from Cisco-IOS-XE-ospf). Every other node belongs to the same module as its parent.
SECTION 4
The same data in XML
XML identifies modules with namespaces instead of names. The rules:
- The first element declares the module’s namespace:
xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native". - An element from another module (augment) declares its own namespace.
- A list is the same element repeated, once per entry. RFC 7950 requires the key leafs first, in the order of the
keystatement, before the other children. - A leaf-list is also a repeated element. When the leaf-list is
ordered-by user, the order of the elements is the order you set. emptyis an element with no content:<shutdown/>.- The value of an
identityrefuses an XML prefix that must be declared on that element.
This XML was generated by yanglint from the JSON above (excerpt):
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<hostname>R1</hostname>
<ip>
<access-list>
<extended xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-acl">
<name>BLOCK-TELNET</name>
<access-list-seq-rule>
<sequence>10</sequence>
<ace-rule>
<action>deny</action>
<protocol>tcp</protocol>
<any/>
<dst-any/>
<dst-eq>telnet</dst-eq>
</ace-rule>
</access-list-seq-rule>
</extended>
</access-list>
</ip>
<vlan>
<vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">
<id>10</id>
<name>USERS</name>
</vlan-list>
<vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">
<id>20</id>
<name>VOICE</name>
</vlan-list>
</vlan>
<interface>
<GigabitEthernet>
<name>2</name>
<description>Uplink to R2</description>
<shutdown/>
</GigabitEthernet>
</interface>
</native>
An identityref from the IETF model, in both encodings:
"type": "iana-if-type:softwareLoopback"
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:softwareLoopback</type>
XML versus JSON, side by side:
| Point | JSON | XML |
|---|---|---|
| Module change | "Cisco-IOS-XE-vlan:vlan-list" | <vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan"> |
| List with 2 entries | One member with an array of 2 objects | The same element twice |
uint64 | "10" (string) | 10 (XML has no types; the text is the same) |
empty | [null] | <shutdown/> |
| Member order | Free | Keys first, then the other children |
NETCONF DIFFERENCE
In an <edit-config>, this XML goes inside <config>. In RESTCONF, the body is the element itself, with no wrapper. NETCONF is covered in TH-REST-005.
SECTION 5
The body depends on the RESTCONF target
In RESTCONF, the JSON starts at the node the URI points to, not always at native. Two rules from RFC 8040 apply:
- For POST, the body must contain exactly one instance of the data resource being created.
- For PUT, the body represents the new resource in full and replaces only that resource.
The same VLAN 10, sent to three targets:
| Method and target | Body (JSON) |
|---|---|
PATCH /restconf/data/Cisco-IOS-XE-native:native | {"Cisco-IOS-XE-native:native": {"vlan": {"Cisco-IOS-XE-vlan:vlan-list": [{"id": 10, "name": "USERS"}]}}} |
PATCH /restconf/data/Cisco-IOS-XE-native:native/vlan | {"Cisco-IOS-XE-native:vlan": {"Cisco-IOS-XE-vlan:vlan-list": [{"id": 10, "name": "USERS"}]}} |
PUT /restconf/data/Cisco-IOS-XE-native:native/vlan/Cisco-IOS-XE-vlan:vlan-list=10 | {"Cisco-IOS-XE-vlan:vlan-list": [{"id": 10, "name": "USERS"}]} |
Three details you can see in the table:
- The first member is always the target node, module-qualified. That is the same top-level rule of RFC 7951, applied at the target.
- A list entry is still sent as a one-element array. RFC 8040 shows this in its own PUT example (
"example-jukebox:album" : [ {...} ]). - The key in the body must match the key in the URI. A PUT to
vlan-list=10with"id": 20in the body is an error.
What each method does with the body (plain PATCH merges, PUT replaces, POST creates) is covered in TH-REST-003.
SECTION 6
YAML: the same tree, with parser traps
YAML is not a YANG encoding. No RFC defines it for YANG data, and RESTCONF does not accept it on the wire. It shows up because Ansible and most source-of-truth files are written in YAML. In practice you write the RFC 7951 JSON tree in YAML syntax and convert it to JSON before sending.
The validated instance from section 3, written in YAML:
Cisco-IOS-XE-native:native:
hostname: R1
vlan:
Cisco-IOS-XE-vlan:vlan-list:
- id: 10
name: USERS
- id: 20
name: VOICE
interface:
GigabitEthernet:
- name: "2"
description: Uplink to R2
shutdown: [null]
ip:
access-list:
Cisco-IOS-XE-acl:extended:
- name: BLOCK-TELNET
access-list-seq-rule:
- sequence: "10"
remarks:
- Block Telnet from anywhere
ace-rule:
action: deny
protocol: tcp
any: [null]
dst-any: [null]
dst-eq: telnet
router:
Cisco-IOS-XE-ospf:router-ospf:
ospf:
process-id:
- id: 1
router-id: 1.1.1.1
Loaded with PyYAML (the YAML library Ansible uses) and converted to JSON, this file passes yanglint unchanged. Keys such as Cisco-IOS-XE-native:native need no quotes: the colon only starts a value when a space follows it.
The traps. PyYAML follows YAML 1.1 and guesses each value’s type from how it is written. A guess that does not match the YANG type breaks the payload. All of these were tested:
| You write | PyYAML reads | Result against the model |
|---|---|---|
name: "2" (interface) | string "2" | Valid |
name: 2 | number 2 | Invalid non-string-encoded string value "2" |
sequence: 10 (ACL, uint64) | number 10 | Invalid: uint64 must be a string |
name: ON (VLAN) | boolean true | Invalid non-string-encoded string value "true" |
name: no | boolean false | Same error |
name: 010 | number 8 (octal) | Wrong value, or a type error |
shutdown: [null] | [None] → JSON [null] | Valid |
shutdown: ~ | None → JSON null | Invalid non-empty-encoded empty value "" |
The rule that avoids all of them: quote every value that must be a string or a 64-bit number ("2", "10", "ON"), and write empty as [null].
In Ansible. ansible.netcommon.restconf_config accepts content in json (the default) or xml. Keep the data as a YAML variable and render it as JSON in the task:
- name: Create VLAN 10 with RESTCONF
ansible.netcommon.restconf_config:
path: /data/Cisco-IOS-XE-native:native/vlan
method: patch
content: "{{ vlan_payload | to_json }}"
vars:
vlan_payload:
Cisco-IOS-XE-native:vlan:
Cisco-IOS-XE-vlan:vlan-list:
- id: 10
name: USERS
The full Ansible workflow (httpapi connection, inventory, idempotency) is in the Ansible track.
SECTION 7
Validate before you send
A device that rejects a payload returns an error, but you only find out after sending it. yanglint checks the instance against the same models offline. It also converts between JSON and XML:
# Validate a configuration instance against the models it uses
yanglint -p ./2621 -t config \
Cisco-IOS-XE-native.yang Cisco-IOS-XE-vlan.yang Cisco-IOS-XE-acl.yang \
Cisco-IOS-XE-ospf.yang payload.json
# Convert the same instance to XML
yanglint -p ./2621 -t config -f xml \
Cisco-IOS-XE-native.yang Cisco-IOS-XE-vlan.yang payload.json
On the IOS XE native model, yanglint may ask for extra modules that the feature modules import (in our tests, Cisco-IOS-XE-vtp and Cisco-IOS-XE-segment-routing). Add them to the command. It also prints warnings about obsolete nodes in the model; those warnings do not invalidate your payload.
Errors from the tests on this page and in TH-REST-001:
| Mistake | yanglint error |
|---|---|
uint64 sent as a number | Invalid non-num64-encoded uint64 value "10" |
empty sent as true | Invalid non-empty-encoded empty value "true" |
| String sent as a number | Invalid non-string-encoded string value "2" |
| Augmented node without its module name (JSON) or namespace (XML) | Node "vlan-list" not found as a child of "vlan" node |
Value outside range | Unsatisfied range - value "4095" is out of the allowed range |
| Two cases of the same choice | Data for both cases "any-case" and "host-case" exist |
What the exam expects
- ✓ Build a JSON instance from a YANG model or a pyang tree (topic 2.7).
- ✓ Build the YAML equivalent and know where YAML type guessing breaks it.
- ✓ Apply the module-qualification rule: on the first member and wherever the module changes.
- ✓ Encode lists as arrays (even with one entry) and leaf-lists as arrays of values.
- ✓ Encode
uint64/int64/decimal64as strings,emptyas[null]andidentityrefwith its module. - ✓ Read and write the XML equivalent: namespaces, repeated elements, keys first,
<shutdown/>. - ✓ Shape the body for the RESTCONF target: the first member is the target node.
- ✓ Interpret a validation or device error and point to the field that caused it.
EXAM CHECK · 350-901 TOPIC 2.7
Read it like the exam
1. A pyang tree shows native/router/ios-ospf:router-ospf/ospf/process-id* [id]. Write the minimal JSON that creates OSPF process 1, starting at native.
{"Cisco-IOS-XE-native:native": {"router": {"Cisco-IOS-XE-ospf:router-ospf": {"ospf": {"process-id": [{"id": 1}]}}}}}. native is the first member, so it is qualified. router-ospf comes from another module, so it is qualified too. process-id is a list, so its value is an array even with one entry.
2. Your Ansible variable has sequence: 10 and the device rejects the ACL. The YAML looks correct. What happened?
sequence is uint64, which RFC 7951 encodes as a JSON string. YAML read 10 as a number, so the JSON carried 10 instead of "10". Quote it: sequence: "10".
3. You PUT to .../Cisco-IOS-XE-vlan:vlan-list=10 with the body {"id": 10, "name": "USERS"}. What is missing?
The top-level member. The body must start with the target node, module-qualified, and a list entry is sent as an array: {"Cisco-IOS-XE-vlan:vlan-list": [{"id": 10, "name": "USERS"}]}.
4. In XML, why does <vlan-list> carry its own xmlns when <native> already declared one?
vlan-list is defined in Cisco-IOS-XE-vlan and augmented into native. In XML, a module is identified by its namespace. Without the declaration the element would belong to the native namespace, where vlan-list does not exist. This is the XML equivalent of "Cisco-IOS-XE-vlan:vlan-list" in JSON.
5. A colleague writes a VLAN named OFF in YAML, and the playbook fails with a type error on name. Why?
YAML 1.1 parsers such as PyYAML read OFF, ON, yes and no as booleans. name is a YANG string, so it received false. Quote the value: name: "OFF".
References
- Cisco. Designing, Deploying and Managing Network Automation Systems v2.0 (350-901) exam topics
- Cisco. CCIE Automation Practical v1.2 exam topics
- Lhotka, L. RFC 7951: JSON Encoding of Data Modeled with YANG. IETF, 2016
- Bjorklund, M. RFC 7950: The YANG 1.1 Data Modeling Language (sections 7.7 and 7.8, XML encoding). IETF, 2016
- Bierman, A., Bjorklund, M., Watsen, K. RFC 8040: RESTCONF Protocol (sections 3.5.3, 4.4–4.6, 5.2). IETF, 2017
- Ansible. ansible.netcommon.restconf_config module
- YangModels. Cisco IOS XE 26.2.1 YANG models
- libyang / yanglint
- PyYAML