Python JSON Handling

Python 8 min min read Updated: Mar 09, 2026 Intermediate
Python JSON Handling
Intermediate Topic 9 of 10

JSON Handling in Python

JSON (JavaScript Object Notation) is a lightweight data format used for storing and exchanging data between applications. It is widely used in web development, APIs, configuration files, and data communication between servers and clients.

Python provides a built-in module called json that allows developers to easily convert Python objects into JSON format and vice versa.

What is JSON?

JSON is a text-based data format that stores data in key-value pairs, similar to Python dictionaries. It is human-readable and commonly used in APIs and web services.

Example of JSON data:

{
  "name": "Alice",
  "age": 25,
  "city": "New York"
}

In Python, this structure is represented as a dictionary.

Importing the JSON Module

To work with JSON in Python, you need to import the built-in json module.

python import json

Converting Python Object to JSON

The json.dumps() function converts a Python object into a JSON string.

python import json data = { "name": "Alice", "age": 25, "city": "New York" } json_data = json.dumps(data) print(json_data)

This converts a Python dictionary into JSON format.

Converting JSON to Python Object

The json.loads() function converts a JSON string into a Python object.

python import json json_data = '{"name": "Alice", "age": 25}' data = json.loads(json_data) print(data["name"])

This converts the JSON string into a Python dictionary.

Writing JSON to a File

Python allows writing JSON data directly into a file using the json.dump() function.

python import json data = { "name": "Alice", "age": 25, "city": "New York" } with open("data.json", "w") as file: json.dump(data, file)

This stores the Python dictionary into a JSON file.

Reading JSON from a File

The json.load() function reads JSON data from a file and converts it into a Python object.

python import json with open("data.json", "r") as file: data = json.load(file) print(data["name"])

This reads the JSON file and converts it into a Python dictionary.

Pretty Printing JSON

Python allows formatting JSON output for better readability using the indent parameter.

python import json data = { "name": "Alice", "age": 25, "skills": ["Python", "Data Science"] } print(json.dumps(data, indent=4))

This produces a well-formatted JSON output.

Python to JSON Data Type Mapping

Python Type JSON Type
dict object
list, tuple array
str string
int, float number
True true
False false
None null

Real-World Example

JSON is commonly used in APIs to exchange data between applications.

python import json user = { "username": "john123", "email": "john@example.com", "active": True } json_string = json.dumps(user) print(json_string)

This converts user information into JSON format for data transmission.

Best Practices for JSON Handling

  • Use JSON for data exchange between applications.
  • Validate JSON data before processing.
  • Use indentation for better readability during debugging.
  • Store structured data using dictionaries and lists.

Conclusion

JSON handling is an essential skill for Python developers, especially when working with APIs, web services, and data storage. Python's built-in json module makes it easy to convert between Python objects and JSON data.

By understanding JSON handling, developers can efficiently exchange and manage structured data in modern applications.

In the next tutorial, we will explore Working with APIs in Python and learn how Python communicates with web services.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators