{ }JSON Workshop/Formatter & Validator
Python Guides7 min read

How to Parse, Modify, and Save JSON Data in Python (Complete Guide)

Master Python built-in json module: Learn json.loads(), json.dumps(), file read and write operations, and error handling.

Written by JSON Workshop Team

Introduction to Python json Module

Python includes a standard library module called json for serializing data structures into JSON strings and deserializing JSON payloads back into Python dictionaries and lists.

No third-party packages are required to work with standard JSON files in Python.


1. Parsing JSON Strings (json.loads)

When receiving a JSON payload from an API request, use json.loads():

import json

raw_json = '{"id": 109, "name": "Python Service", "status": "healthy"}'
data = json.loads(raw_json)

print(data["name"])  # Outputs: Python Service

2. Converting Python Dicts to JSON (json.dumps)

Convert a Python dictionary into a formatted JSON string:

import json

user_data = {"user_id": 8821, "roles": ["admin"], "is_active": True}
json_string = json.dumps(user_data, indent=4)
print(json_string)

3. Reading and Writing JSON Files

To read or write a .json file stored on disk:

import json

# Write JSON
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(user_data, f, indent=2)

# Read JSON
with open('data.json', 'r', encoding='utf-8') as f:
    loaded_data = json.load(f)

To test and fix malformed JSON before parsing it in Python, use our client-side JSON Formatter & Validator.

Need to format or validate JSON?

Try our 100% private, client-side JSON Formatter & Validator tool.

Open JSON Formatter →