Working with JSON Files in Python

Loading

JSON (JavaScript Object Notation) is a lightweight, human-readable format for storing and exchanging data. Python provides built-in support for JSON through the json module.

Why use JSON?

  • Widely used in web APIs and data storage.
  • Easily readable by humans and machines.
  • Language-independent and supported across programming languages.

1. Importing the JSON Module

import json

2. JSON Data Format

A JSON object is similar to a Python dictionary and consists of key-value pairs.

Example JSON Data (String Format)

{
"name": "Alice",
"age": 25,
"city": "New York",
"languages": ["English", "Spanish"]
}

3. Converting Between JSON and Python Objects

Method 1: Converting JSON String to Python Object (json.loads)

json.loads() parses a JSON string and converts it into a Python dictionary.

import json

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

# Convert JSON string to Python dictionary
python_dict = json.loads(json_data)

print(python_dict) # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(python_dict["name"]) # Output: Alice

Use this method when receiving JSON data as a string (e.g., from an API).


Method 2: Converting Python Dictionary to JSON String (json.dumps)

json.dumps() converts a Python dictionary into a JSON string.

import json

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

# Convert Python dictionary to JSON string
json_string = json.dumps(data)

print(json_string) # Output: '{"name": "Alice", "age": 25, "city": "New York"}'

dumps() is useful when sending data to APIs or storing JSON in files.


4. Reading and Writing JSON Files

Method 1: Reading a JSON File (json.load)

import json

# Open and read JSON file
with open("data.json", "r") as file:
data = json.load(file)

print(data) # Output: Dictionary representation of JSON

Use json.load() for reading JSON files.


Method 2: Writing to a JSON File (json.dump)

import json

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

# Write Python dictionary to JSON file
with open("data.json", "w") as file:
json.dump(data, file)

print("Data saved to data.json")

Use json.dump() to write Python dictionaries to a JSON file.


5. Formatting JSON Output (indent, sort_keys)

Pretty-Printing JSON

indent makes JSON easier to read, and sort_keys=True sorts keys alphabetically.

import json

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

formatted_json = json.dumps(data, indent=4, sort_keys=True)

print(formatted_json)

Output (Formatted JSON):

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

Useful for debugging and saving structured JSON data.


6. Working with Nested JSON

Example: Parsing Nested JSON

import json

json_data = '''
{
"name": "Alice",
"details": {
"age": 25,
"city": "New York",
"languages": ["English", "Spanish"]
}
}
'''

data = json.loads(json_data)

# Access nested values
print(data["details"]["city"]) # Output: New York
print(data["details"]["languages"]) # Output: ['English', 'Spanish']

Use ["key"] to access values inside nested dictionaries.


7. Appending Data to a JSON File

To append new data, read the file, update the dictionary, and write back.

import json

# Read existing data
with open("data.json", "r") as file:
data = json.load(file)

# Append new data
data["email"] = "alice@example.com"

# Write updated data back to file
with open("data.json", "w") as file:
json.dump(data, file, indent=4)

print("Data updated successfully!")

JSON files must be read first before modifying them.


8. Handling JSON Errors with try-except

Avoid program crashes due to malformed JSON.

import json

try:
with open("data.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
print("Error: File not found!")
except json.JSONDecodeError:
print("Error: Invalid JSON format!")

json.JSONDecodeError is raised when JSON is not formatted correctly.


9. Converting JSON to a Pandas DataFrame

For data analysis, convert JSON into a Pandas DataFrame.

import json
import pandas as pd

json_data = '''
[
{"name": "Alice", "age": 25, "city": "New York"},
{"name": "Bob", "age": 30, "city": "London"}
]
'''

data = json.loads(json_data)

df = pd.DataFrame(data) # Convert JSON list to DataFrame

print(df)

Best for working with structured JSON in data science projects.


10. Checking If a JSON File Exists

Before reading a file, check if it exists.

import os

if os.path.exists("data.json"):
print("File exists")
else:
print("File not found")

11. Summary Table

OperationFunctionExample
Convert JSON string to dictjson.loads()data = json.loads(json_string)
Convert dict to JSON stringjson.dumps()json_string = json.dumps(data)
Read JSON filejson.load()data = json.load(file)
Write JSON to filejson.dump()json.dump(data, file)
Pretty-print JSONjson.dumps(indent=4)print(json.dumps(data, indent=4))
Handle nested JSONdata["key"]["subkey"]data["details"]["age"]
Append to JSON fileModify dict and use json.dump()data["new_key"] = "value"

Leave a Reply

Your email address will not be published. Required fields are marked *