Parsing JSON in Python

This post covers how to parse JSON in Python using the built-in json module. It walks through the four core functions: json.loads(), json.load(), json.dumps(), and json.dump(), with practical code examples including nested JSON structures.

Parsing JSON in Python

If you've worked with APIs, configuration files, or web data, you've almost certainly encountered JSON. It's everywhere. And Python makes working with it remarkably straightforward once you understand the basics. In this post, we'll cover how to parse JSON in Python from start to finish.

What Is JSON?

JSON stands for JavaScript Object Notation. Despite the name, it's a language-agnostic data format used to exchange structured data between systems. You'll see it in REST API responses, configuration files, log outputs, and much more.

A basic JSON structure looks like this:

{
  "hostname": "router01",
  "vendor": "Cisco",
  "interfaces": ["GigabitEthernet0/0", "GigabitEthernet0/1"],
  "active": true,
  "uptime_days": 42
}

JSON supports strings, numbers, booleans, arrays, objects, and null values. If you've used Python dictionaries, this structure will feel very familiar.

The Python json Module

Python includes a built-in json module in its standard library. No installation needed. You just import it at the top of your script:

import json

The module gives you four core functions. The two you'll use most often are json.loads() and json.dumps(). The other two, json.load() and json.dump(), work directly with files.

Parsing a JSON String with json.loads()

The most common scenario is receiving JSON as a string, perhaps from an API response, and converting it into a Python dictionary. That's what json.loads() does. The "s" stands for "string".

import json

json_string = '{"hostname": "router01", "vendor": "Cisco", "uptime_days": 42}'

device = json.loads(json_string)

print(type(device))        # <class 'dict'>
print(device["hostname"])  # router01
print(device["uptime_days"])  # 42

Once parsed, you access values just like any Python dictionary. JSON objects become Python dicts, JSON arrays become Python lists, and JSON booleans become Python True or False.

Reading JSON from a File with json.load()

If your JSON data lives in a file, use json.load() (without the "s") and pass it an open file object:

import json

with open("device.json", "r") as f:
    device = json.load(f)

print(device["vendor"])  # Cisco

The with statement handles closing the file automatically, which is good practice in Python.

Converting Python to JSON with json.dumps()

You'll also need to go in the other direction: converting a Python dictionary back into a JSON string. Use json.dumps() for this:

import json

device = {
    "hostname": "switch01",
    "vendor": "Cisco",
    "active": True,
    "ports": 48
}

json_string = json.dumps(device, indent=4)
print(json_string)

The indent=4 argument formats the output with indentation, making it human-readable. Without it, you get a compact single-line string. Use the compact version when sending data over a network, and the indented version when writing to a file for humans to read.

Writing JSON to a File with json.dump()

To write a Python object directly to a JSON file, use json.dump():

import json

device = {"hostname": "switch01", "vendor": "Cisco", "ports": 48}

with open("output.json", "w") as f:
    json.dump(device, f, indent=4)

Handling Nested JSON

Real-world JSON is rarely flat. You'll often encounter nested structures. Python handles this naturally since nested JSON objects simply become nested dictionaries:

import json

json_string = '''
{
  "device": "router01",
  "interfaces": {
    "GigabitEthernet0/0": {"ip": "192.168.1.1", "status": "up"},
    "GigabitEthernet0/1": {"ip": "10.0.0.1", "status": "down"}
  }
}
'''

data = json.loads(json_string)
print(data["interfaces"]["GigabitEthernet0/0"]["ip"])  # 192.168.1.1

You chain dictionary keys to drill into nested data. This pattern comes up constantly when working with API responses.

A Quick Reference: The Four Key Functions

  • json.loads(string): parse a JSON string into a Python object
  • json.load(file): parse JSON directly from an open file
  • json.dumps(object): convert a Python object to a JSON string
  • json.dump(object, file): write a Python object as JSON to a file

What's Next

Now that you can parse and produce JSON in Python, the natural next step is working with real API responses. In the next post, we'll make HTTP requests using the requests library, pull live JSON data from a public API, and use everything you've learned here to extract and display the results.