JSON in REST APIs: How It All Fits Together

This post explains how JSON and REST APIs work together, covering HTTP methods, request and response structures, and how to parse JSON responses in Python. It uses practical networking examples to make the concepts concrete for beginners.

JSON in REST APIs: How It All Fits Together

If you have spent any time working with web services or network automation tools, you have probably seen JSON showing up everywhere. But understanding why JSON is so closely tied to REST APIs, and how they actually work together, is a key milestone in your programming journey. Let us break it down clearly.

What Is a REST API?

A REST API (Representational State Transfer Application Programming Interface) is a standardised way for two systems to communicate over HTTP. Think of it like a waiter in a restaurant: your application places an order (a request), and the API returns what you asked for (a response). REST APIs use standard HTTP methods to perform operations:

  • GET: Retrieve data from a server
  • POST: Send new data to a server
  • PUT: Update existing data on a server
  • DELETE: Remove data from a server

These APIs are used everywhere, from social media platforms to network devices. Cisco's DNA Center, for example, exposes a REST API that lets you query and configure your network programmatically.

Where Does JSON Fit In?

When your application sends a request to a REST API, it needs a common language to exchange data. JSON (JavaScript Object Notation) has become the dominant format for this exchange because it is lightweight, human-readable, and easy for programming languages to parse.

When you make a GET request to an API, the server typically sends back a JSON response. When you make a POST or PUT request, you usually send a JSON body along with your request. Both sides agree to speak JSON, which keeps things simple and consistent.

A Practical Example

Imagine you are querying a network management API to get a list of devices. You might make a GET request to an endpoint like this:

GET https://api.example.com/v1/devices
Accept: application/json
Authorization: Bearer your_token_here

The server processes your request and sends back a JSON response like this:

{
  "devices": [
    {
      "id": "001",
      "hostname": "core-sw-01",
      "ip_address": "10.0.0.1",
      "status": "online"
    },
    {
      "id": "002",
      "hostname": "dist-sw-01",
      "ip_address": "10.0.0.2",
      "status": "offline"
    }
  ],
  "total": 2
}

Notice a few things here. The Accept: application/json header tells the server that you want JSON back. The response body is a structured JSON object containing a list of devices, each with its own properties.

Sending JSON in a Request Body

When you want to create or update a resource, you send JSON in the request body. Here is an example POST request to add a new device:

POST https://api.example.com/v1/devices
Content-Type: application/json
Authorization: Bearer your_token_here

{
  "hostname": "access-sw-01",
  "ip_address": "10.0.0.3",
  "location": "Floor 2"
}

The Content-Type: application/json header tells the server that the body you are sending is formatted as JSON. The server then reads that JSON, processes it, and typically returns a JSON response confirming what was created.

Working with JSON Responses in Python

In Python, the requests library makes this incredibly straightforward. After making a request, you can call .json() on the response to automatically parse the JSON into a Python dictionary:

import requests

response = requests.get(
    "https://api.example.com/v1/devices",
    headers={"Authorization": "Bearer your_token_here"}
)

data = response.json()

for device in data["devices"]:
    print(device["hostname"], device["ip_address"])

This would output:

core-sw-01 10.0.0.1
dist-sw-01 10.0.0.2

The .json() method handles all the parsing for you, turning the raw JSON string into a native Python dictionary that you can work with immediately.

Why JSON Won Over Other Formats

You might wonder why JSON became the standard rather than XML or other formats. The honest answer is practicality. JSON is less verbose than XML, maps naturally to data structures found in most programming languages (like Python dictionaries and lists), and is easy to read when you are debugging. Most modern APIs default to JSON, and many have dropped XML support entirely.

What's Next

Now that you understand how JSON and REST APIs work together, the natural next step is learning how to handle errors in API responses. Status codes like 200, 404, and 500 tell you a lot about what happened with your request, and writing code that responds to them correctly is an essential skill. We will cover that in the next post in this series.

🔧
If you are learning to interact with REST APIs, Postman or Insomnia are excellent tools for testing your GET, POST, PUT, and DELETE requests visually before writing a single line of Python code. They let you inspect JSON responses, set headers like Authorization and Content-Type, and save your API calls for reuse. Postman, Insomnia and curl.