Python tutorials > Working with External Resources > File I/O > How to work with JSON?
How to work with JSON?
Working with JSON in Python
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Python's This tutorial covers how to read, write, and manipulate JSON data using Python.json
module provides a straightforward way to encode and decode JSON data.
Importing the json
Module
Before working with JSON data, you need to import the json
module. This module provides functions for encoding and decoding JSON data.
import json
Reading JSON Data from a File
To read JSON data from a file, use the The code snippet provided defines a function Example data.json:json.load()
function. This function takes a file object as input and returns a Python dictionary or list representing the JSON data.read_json_file
that takes a file path as input, attempts to open the file in read mode ('r'
), and then uses json.load()
to parse the JSON data. It includes error handling for file not found and invalid JSON format.{
"name": "John Doe",
"age": 30,
"city": "New York"
}
import json
def read_json_file(filepath):
try:
with open(filepath, 'r') as f:
data = json.load(f)
return data
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
return None
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {filepath}")
return None
# Example Usage
data = read_json_file('data.json')
if data:
print(data)
Writing JSON Data to a File
To write JSON data to a file, use the The code snippet defines a function json.dump()
function. This function takes a Python dictionary or list, a file object, and an optional indent
parameter to format the output JSON.write_json_file
that takes a file path and the data to be written as input. It opens the file in write mode ('w'
), uses json.dump()
to write the data to the file, and formats the JSON output with an indent of 4 spaces for readability. Error handling is included.
import json
def write_json_file(filepath, data):
try:
with open(filepath, 'w') as f:
json.dump(data, f, indent=4)
print(f"Data written to {filepath} successfully.")
except Exception as e:
print(f"Error writing to file: {e}")
# Example Usage
data = {
'name': 'Jane Doe',
'age': 25,
'city': 'Los Angeles'
}
write_json_file('output.json', data)
Loading JSON Data from a String
To load JSON data from a string, use the json.loads()
function (note the 's' at the end, standing for 'string'). This function takes a JSON string as input and returns a Python dictionary or list.
import json
json_string = '{"name": "Peter", "age": 40, "city": "Chicago"}'
data = json.loads(json_string)
print(data)
Dumping JSON Data to a String
To dump JSON data to a string, use the json.dumps()
function (again, note the 's' at the end). This function takes a Python dictionary or list and returns a JSON string. The indent
parameter allows for pretty-printing the JSON string.
import json
data = {
'name': 'Alice',
'age': 35,
'city': 'San Francisco'
}
json_string = json.dumps(data, indent=4)
print(json_string)
Concepts Behind the Snippet
The core concept is the conversion between Python data structures (dictionaries, lists, strings, numbers, booleans, and None) and JSON data. json.load()
and json.loads()
convert JSON to Python, while json.dump()
and json.dumps()
convert Python to JSON. Understanding this mapping is crucial for effectively using the json
module.
Real-Life Use Case
Many web APIs return data in JSON format. For example, you might use Python to make a request to a weather API, which would return the weather data as a JSON string. Your Python code would then use json.loads()
to parse the JSON string and extract the relevant weather information.
Best Practices
try...except
blocks) when reading and writing JSON files. This helps to gracefully handle cases where the file doesn't exist or the JSON is invalid.indent
parameter in json.dump()
to format the JSON output for readability.
Interview Tip
Be prepared to discuss the differences between json.load()
and json.loads()
, as well as json.dump()
and json.dumps()
. Also, be ready to talk about error handling and best practices for working with JSON data.
When to Use JSON
JSON is an excellent choice for:
Memory Footprint
The memory footprint depends on the size of the JSON data. Large JSON files can consume a significant amount of memory when loaded into Python. Consider using streaming approaches (e.g., reading the JSON data in chunks) if you're working with extremely large files.
Alternatives
Alternatives to JSON include:
Pros of JSON
Cons of JSON
FAQ
-
What is the difference between
json.load()
andjson.loads()
?
json.load()
reads JSON data from a file, whilejson.loads()
reads JSON data from a string. -
How do I handle errors when reading a JSON file?
Use atry...except
block to catchFileNotFoundError
andjson.JSONDecodeError
. -
How do I format JSON output for readability?
Use theindent
parameter injson.dump()
. -
Why is my output not printing special character correctly?
Ensure you specify the 'encoding' attribute when opening files for reading or writing to UTF-8 (encoding='utf-8').