6.6 JSON-Encoded Data
Key Takeaways
- JSON (JavaScript Object Notation) is the most common data format for REST APIs and network automation.
- JSON uses key-value pairs enclosed in curly braces {} for objects and square brackets [] for arrays.
- JSON supports strings, numbers, booleans, null, objects, and arrays as data types.
- JSON is human-readable and machine-parseable, making it ideal for automation.
- YAML is an alternative to JSON, commonly used in Ansible playbooks — more human-readable but whitespace-sensitive.
JSON-Encoded Data
JSON (JavaScript Object Notation) is the standard data format for REST APIs and network automation. The CCNA requires you to read and interpret JSON data.
JSON Syntax Rules
- Data is in key:value pairs
- Data is separated by commas
- Curly braces {} hold objects
- Square brackets [] hold arrays (ordered lists)
- Keys must be strings (in double quotes)
- Values can be strings, numbers, booleans, null, objects, or arrays
JSON Data Types
| Type | Example | Description |
|---|---|---|
| String | "hostname": "SW1" | Text in double quotes |
| Number | "vlan_id": 10 | Integer or decimal (no quotes) |
| Boolean | "enabled": true | true or false (no quotes) |
| Null | "description": null | Empty/no value |
| Object | {"name": "SW1"} | Nested key-value pairs in {} |
| Array | [10, 20, 30] | Ordered list in [] |
JSON Examples for Networking
Simple Device Object
{
"hostname": "Core-SW1",
"management_ip": "10.0.0.1",
"platform": "Catalyst 9300",
"software_version": "17.9.4",
"serial_number": "FCW2145L0KP",
"uptime_days": 45,
"reachable": true
}
Array of VLANs
{
"vlans": [
{
"id": 10,
"name": "SALES",
"state": "active",
"ports": ["Gi0/1", "Gi0/2", "Gi0/3"]
},
{
"id": 20,
"name": "ENGINEERING",
"state": "active",
"ports": ["Gi0/4", "Gi0/5", "Gi0/6"]
},
{
"id": 30,
"name": "MANAGEMENT",
"state": "active",
"ports": ["Gi0/7"]
}
]
}
Nested OSPF Configuration
{
"ospf": {
"process_id": 1,
"router_id": "1.1.1.1",
"areas": [
{
"area_id": 0,
"networks": [
{
"network": "10.0.0.0",
"wildcard": "0.0.0.3"
},
{
"network": "192.168.10.0",
"wildcard": "0.0.0.255"
}
]
}
],
"passive_interfaces": ["GigabitEthernet0/1"],
"reference_bandwidth": 10000,
"default_information_originate": true
}
}
Reading JSON — Practice
Given this JSON, answer:
{
"interfaces": [
{
"name": "GigabitEthernet0/0",
"ip_address": "10.0.0.1",
"subnet_mask": "255.255.255.252",
"status": "up",
"protocol": "up",
"speed": "1000 Mbps",
"duplex": "full"
},
{
"name": "GigabitEthernet0/1",
"ip_address": "192.168.10.1",
"subnet_mask": "255.255.255.0",
"status": "up",
"protocol": "up",
"speed": "1000 Mbps",
"duplex": "full"
}
]
}
-
Q: What is the IP address of GigabitEthernet0/1?
-
A: 192.168.10.1
-
Q: How many interfaces are in the array?
-
A: 2 (the array has two objects)
-
Q: What is the subnet mask of the first interface?
-
A: 255.255.255.252
JSON vs. YAML vs. XML
| Feature | JSON | YAML | XML |
|---|---|---|---|
| Format | Key-value, curly braces | Key-value, indentation | Tags (like HTML) |
| Readability | Good | Best | Verbose |
| Used by | REST APIs, JavaScript | Ansible playbooks | Legacy APIs, SOAP |
| Whitespace | Ignored | Significant (indentation matters) | Ignored |
| Comments | Not supported | Supported (#) | Supported (<!-- -->) |
On the Exam: You will likely see a JSON output and be asked to identify specific values. Practice reading nested JSON structures — know how to find values in objects and arrays.
In JSON, which characters are used to denote an array (ordered list)?
Given this JSON: {"device": {"hostname": "R1", "interfaces": [{"name": "Gi0/0", "ip": "10.0.0.1"}, {"name": "Gi0/1", "ip": "10.0.1.1"}]}} — What is the IP address of Gi0/1?
Which data format is most commonly used for REST API responses in network automation?