Chapter 26 of 29
JSON, Data Models, and REST APIs in Network Automation
Peek into the data formats and APIs that let software talk to your network, focusing on JSON and REST as used in CCNA-level automation tasks.
Big Picture: Why JSON and REST Matter in Network Automation
From CLI to APIs
Modern networks use APIs so software can talk to devices and controllers, not just humans on the CLI. For CCNA, the key ideas are JSON data and REST-style web APIs.
REST API Definition
A Representational State Transfer (REST) API is a web-based interface that uses HTTP methods and resource-oriented URIs to enable programmatic access to network devices and controllers.
Your Learning Goals
You will learn to read JSON, understand what REST and HTTP methods mean conceptually, and interpret simple request/response examples like those shown on the CCNA exam.
JSON Basics: Structure, Syntax, and Terminology
What Is JSON?
JSON is a text format for structured data. It uses objects in `{ }` and arrays in `[ ]`. It is the default way many network APIs send and receive information.
Objects and Keys
An object is a set of name/value pairs: keys in double quotes on the left of `:`, values on the right. Pairs are separated by commas, with no comma after the last pair.
Example: Interface JSON
Example object: name, description, enabled, mtu, and nested ipv4 info. Learn to locate specific fields like the IP address or interface name inside such structures.
Exam-Relevant Syntax
Remember: double quotes for strings, lowercase true/false/null, commas between items, and no trailing commas. CCNA questions often test your ability to spot valid vs invalid JSON.
Reading JSON: Network Device Inventory Example
Inventory JSON Overview
The example JSON has an outer object with `devices` (an array) and `total` (a number). Each entry in `devices` is one network device.
Interpreting Device Entries
Each device object has fields like hostname, mgmt_ip, role, os, and up. You read them just like a table: each key is a column; each object is a row.
Answering Questions from JSON
To find which device is down, scan for `"up": false`. To count devices, either read the `total` field or count objects in the `devices` array.
REST API Fundamentals: Resources, URIs, and Representations
REST Definition Reminder
A REST API is a web-based interface that uses HTTP methods and resource-oriented URIs to enable programmatic access to network devices and controllers.
Resources and URIs
Resources are things like devices or interfaces. URIs identify them, for example `/api/v1/devices` or `/api/v1/devices/branch-rtr-1/interfaces`.
Representations (Often JSON)
When you request a resource, the API returns a representation of its state, usually JSON. You act on resources with HTTP methods and read the JSON response.
Exam-Focused View
On CCNA, you mainly need to recognize resources in URIs and understand that JSON responses describe their current state, not design the API yourself.
HTTP Methods in REST: GET, POST, PUT, DELETE
GET: Read-Only
GET is used to read data, like `GET /api/v1/devices`. It should not change anything on the server. Think of it as a "show" command over HTTP.
POST: Create or Trigger
POST usually creates new resources or triggers actions, sending JSON in the body. Example: creating a new VLAN with `POST /api/v1/vlans`.
PUT and DELETE
PUT updates or replaces an existing resource at a known URI. DELETE removes a resource, such as `DELETE /api/v1/vlans/10` to delete VLAN 10.
Avoiding Method Confusion
On exams, always pair the method with the URI. GET + /devices means list devices; DELETE + /vlans/10 means remove VLAN 10.
Putting It Together: Simple REST API Request and Response
This step shows a full example of a REST API request and the JSON response, similar to what you might see in CCNA questions. Focus on matching method, URI, and the JSON fields.
Example: Retrieve interface details for a specific device.
HTTP request (conceptual view):
```http
GET /api/v1/devices/branch-rtr-1/interfaces HTTP/1.1
Host: controller.example.com
Accept: application/json
Authorization: Bearer <token>
```
Possible JSON response:
```json
{
"device": "branch-rtr-1",
"interfaces": [
{
"name": "GigabitEthernet0/0",
"description": "Uplink to ISP",
"enabled": true,
"ipv4": {
"address": "203.0.113.2",
"mask": "255.255.255.252"
}
},
{
"name": "GigabitEthernet0/1",
"description": "LAN",
"enabled": true,
"ipv4": {
"address": "192.0.2.1",
"mask": "255.255.255.0"
}
}
],
"count": 2
}
```
How to reason about this:
- Method: `GET` means "read".
- URI: `/api/v1/devices/branch-rtr-1/interfaces` means "interfaces for device branch-rtr-1".
- Response JSON:
- `device` confirms which device.
- `interfaces` is an array of interface objects.
- `count` tells you how many interfaces are returned.
Typical exam-style questions:
- "Which IP address is configured on the LAN interface?" Answer: `192.0.2.1`.
- "How many interfaces did the API return?" Answer: `2` (from `count` or array length).
Thought Exercise: Map REST Calls to CLI Tasks
Work through this mentally to connect REST concepts with familiar CLI operations.
Imagine you are managing a switch called `branch-sw-1`. For each CLI-style task, decide which HTTP method and URI would conceptually match it in a REST API.
- Task A: Show all VLANs configured on the switch.
- CLI: `show vlan brief`
- REST: Which method and resource?
- Hint: You are only reading data, not changing it.
- Task B: Create VLAN 20 with name `USERS`.
- CLI: `vlan 20`, `name USERS`
- REST: Which method and resource path might you use? Would you send JSON in the body?
- Task C: Change the description of interface Gi0/1.
- CLI: `interface Gi0/1`, `description Uplink to core`
- REST: Are you creating a new interface or updating an existing one? Which method fits that idea?
- Task D: Remove VLAN 30.
- CLI: `no vlan 30`
- REST: Which method corresponds to removing a resource?
When you are ready, compare your reasoning to the suggested mappings:
- Task A: likely `GET /api/v1/devices/branch-sw-1/vlans`
- Task B: likely `POST /api/v1/devices/branch-sw-1/vlans` with JSON describing VLAN 20
- Task C: likely `PUT /api/v1/devices/branch-sw-1/interfaces/Gi0/1` with JSON containing the new description
- Task D: likely `DELETE /api/v1/devices/branch-sw-1/vlans/30`
The exact URIs vary by vendor, but the method semantics (GET=read, POST=create, PUT=update, DELETE=remove) remain consistent and are what CCNA tests.
Quiz 1: JSON and HTTP Methods
Test your understanding of JSON structure and REST methods.
You see the following API call in a CCNA question: `POST /api/v1/vlans` The request body (in JSON) describes VLAN 50. Conceptually, what is this call trying to do?
- Retrieve the configuration of VLAN 50 from the device
- Create VLAN 50 on the device
- Delete VLAN 50 from the device
- Rename VLAN 50 to a new VLAN ID
Show Answer
Answer: B) Create VLAN 50 on the device
The method is POST, which is typically used to create a new resource or trigger an action. The URI `/api/v1/vlans` points to the VLAN collection, and the JSON body describes VLAN 50, so the intent is to create VLAN 50. GET would retrieve, DELETE would remove, and renaming would usually be modeled as an update (PUT/PATCH) rather than POST.
Quiz 2: Interpreting a JSON Response
Use the JSON to answer the question.
Given this JSON response: ```json { "device": "branch-sw-1", "vlans": [ { "id": 10, "name": "SERVERS", "active": true }, { "id": 20, "name": "USERS", "active": true }, { "id": 30, "name": "GUEST", "active": false } ] } ``` Which statement is correct?
- VLAN 30 is currently inactive on branch-sw-1
- VLAN 20 is missing an ID field
- The JSON is invalid because vlans must be an object, not an array
- The device field should be inside each VLAN object to be valid JSON
Show Answer
Answer: A) VLAN 30 is currently inactive on branch-sw-1
The `vlans` key holds an array of VLAN objects. VLAN 30 has `"active": false`, so it is inactive. VLAN 20 clearly has an `id` field. Arrays are valid JSON values, so `vlans` being an array is fine. The `device` field at the top level is also valid; JSON does not require it to be repeated inside each VLAN.
Key Term Review: JSON and REST
Flip through these cards to reinforce core terms and ideas before moving on.
- JSON object
- An unordered collection of name/value pairs wrapped in `{ }`. Each pair has a key (string in double quotes) and a value (string, number, boolean, null, object, or array).
- JSON array
- An ordered list of values wrapped in `[ ]`. Each value can be any JSON type, including objects. Often used to represent lists of devices, interfaces, or VLANs.
- Key (property name) in JSON
- The string on the left side of `:` in a JSON object. It must be in double quotes and uniquely identifies each value within that object.
- REST API (canonical definition)
- A Representational State Transfer (REST) API is a web-based interface that uses HTTP methods and resource-oriented URIs to enable programmatic access to network devices and controllers.
- HTTP GET (in REST)
- A method used to read or retrieve representations of resources, such as device lists or interface details. It should not modify the resource state.
- HTTP POST (in REST)
- A method commonly used to create new resources or trigger actions on the server, usually sending data in the request body as JSON.
- HTTP PUT (in REST)
- A method used to update or replace an existing resource at a known URI, often sending a full or partial JSON representation of the new desired state.
- HTTP DELETE (in REST)
- A method used to remove a resource identified by the URI, such as deleting a specific VLAN or policy.
- Resource-oriented URI
- A URI that identifies a resource, such as `/api/v1/devices` or `/api/v1/devices/branch-rtr-1/interfaces`, used together with HTTP methods in REST APIs.
- Representation (in REST)
- The format used to describe the current state of a resource when it is transferred over the API, commonly JSON in network automation.
Common Exam Patterns and Traps with JSON and REST
Method + URI Together
Never look at the HTTP method or the URI alone. Combine them: the method tells you the action, the URI tells you which resource the action targets.
JSON Nesting Awareness
Pay attention to `{ }` and `[ ]`. Know which keys are at the top level and which are inside arrays or nested objects before answering questions.
Ignore Unnecessary Noise
Headers and tokens often appear but are not the focus at CCNA level. Concentrate on method, URI, and the JSON structure and values.
Practice Makes It Natural
As you see more examples in diagnostics and mock exams, reading JSON and REST calls will feel like reading familiar show command output.
Key Terms
- GET
- An HTTP method used to read or retrieve information about resources without modifying them.
- PUT
- An HTTP method used to update or replace an existing resource at a known URI.
- URI
- Uniform Resource Identifier; the path that uniquely identifies a resource in a REST API, such as `/api/v1/devices`.
- JSON
- JavaScript Object Notation, a text-based data format using objects and arrays to represent structured data, commonly used in network automation APIs.
- POST
- An HTTP method commonly used to create new resources or trigger server-side actions, usually sending data in the request body.
- DELETE
- An HTTP method used to remove a resource identified by the URI.
- REST API
- A Representational State Transfer (REST) API is a web-based interface that uses HTTP methods and resource-oriented URIs to enable programmatic access to network devices and controllers.
- Resource
- In REST, a logical object such as a device, interface, VLAN, or policy that can be accessed and manipulated via URIs.
- JSON array
- A JSON structure wrapped in `[ ]` representing an ordered list of values, such as a list of devices or interfaces.
- HTTP method
- An action verb in HTTP (such as GET, POST, PUT, DELETE) that indicates what operation a client wants to perform on a resource.
- JSON object
- A JSON structure wrapped in `{ }` containing name/value pairs known as keys and values.
- Representation
- The format (often JSON) used to describe the state of a resource when transferred over a REST API.