JSON (JavaScript Object Notation) manipulation in Python
JSON (JavaScript Object Notation) manipulation in Python
JSON (JavaScript Object Notation) manipulation in Python involves working with JSON data, which is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate.
In Python, you can manipulate JSON data using the json module, which provides methods for encoding and decoding JSON data. Here are some common JSON manipulation tasks in Python:
- Loading JSON Data:
- The
json.loads()function is used to parse a JSON string and convert it into a Python data structure (usually a dictionary or a list).
1 2 3 4 5
import json json_string = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}' data = json.loads(json_string) print(data)
- The
- Reading JSON from a File:
- The
json.load()function is used to read JSON data from a file and convert it into a Python data structure.
1 2 3 4 5
import json with open('data.json', 'r') as file: data = json.load(file) print(data)
- The
- Dumping JSON Data:
- The
json.dumps()function is used to convert a Python object (usually a dictionary or a list) into a JSON formatted string.
1 2 3 4 5
import json data = {"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"} json_string = json.dumps(data) print(json_string)
- The
- Writing JSON to a File:
- The
json.dump()function is used to write JSON data to a file.
1 2 3 4 5
import json data = {"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"} with open('output.json', 'w') as file: json.dump(data, file)
- The
- Accessing JSON Data:
- Once you’ve loaded JSON data, you can access individual elements using dictionary or list indexing.
1 2 3 4 5 6
import json json_string = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}' data = json.loads(json_string) print(data['name']) # Output: Vaibhav Karad print(data['age']) # Output: 30
- Modifying JSON Data:
- After loading JSON data, you can modify it like any other Python data structure.
1 2 3 4 5 6 7 8 9 10
import json json_string = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}' data = json.loads(json_string) data['age'] = 31 # Modify the age data['city'] = 'San Francisco' # Modify the city # Convert the modified data back to JSON string modified_json_string = json.dumps(data)
More detailed Examples
Examples of how to parse and manipulate JSON data in Python, along with explanations and sample outputs.
Example 1: Parsing JSON Data
1
2
3
4
5
6
7
8
9
10
11
import json
# JSON data as a string
json_data = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing elements in the dictionary
print(data['name']) # Output: Vaibhav Karad
print(data['age']) # Output: 30
Explanation: In this example, we have a JSON string json_data representing a person’s information. We use json.loads() to parse this string into a Python dictionary called data. We then access individual elements within the dictionary using keys.
Output:
1
2
Vaibhav Karad
27
Example 2: Modifying JSON Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import json
# JSON data as a string
json_data = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Modify data
data['age'] = 31
data['city'] = 'San Francisco'
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data) # Output: {"name": "Vaibhav Karad", "age": 31, "city": "San Francisco"}
Explanation: In this example, we first parse a JSON string into a dictionary. Then, we modify the age and city values. After the modifications, we convert the updated dictionary back to a JSON string using json.dumps().
Output:
1
{"name": "Vaibhav Karad", "age": 31, "city": "San Francisco"}
Example 3: Adding a New Key-Value Pair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import json
# JSON data as a string
json_data = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Add a new key-value pair
data['occupation'] = 'Engineer'
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data)
# Output: {"name": "Vaibhav Karad", "age": 27, "city": "Mumbai", "occupation": "Engineer"}
Explanation: Here, we parse a JSON string into a dictionary, then add a new key-value pair ('occupation': 'Engineer'). We then convert the updated dictionary back to a JSON string.
Output:
1
{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai", "occupation": "Engineer"}
Example 4: Removing a Key-Value Pair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import json
# JSON data as a string
json_data = '{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Remove a key-value pair
del data['age']
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data) # Output: {"name": "Vaibhav Karad", "city": "New York"}
Explanation: In this example, we parse a JSON string into a dictionary and then remove the key 'age'. We then convert the modified dictionary back to a JSON string.
Output:
1
{"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}
Example 5: Working with Nested JSON
1
2
3
4
5
6
7
8
9
10
11
import json
# JSON data as a string with nested structure
json_data = '{"person": {"name": "Vaibhav Karad", "age": 27, "city": "Mumbai"}}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing nested elements
print(data['person']['name']) # Output: Vaibhav Karad
print(data['person']['age']) # Output: 27
Explanation: Here, we have a JSON string with a nested structure. We parse it into a dictionary and then access elements within the nested structure using multiple keys.
Output:
1
2
Vaibhav Karad
27
Example 6: Handling JSON Arrays
1
2
3
4
5
6
7
8
9
10
11
import json
# JSON data as a string with an array
json_data = '{"fruits": ["apple", "banana", "cherry"]}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing elements in the array
print(data['fruits'][0]) # Output: apple
print(data['fruits'][2]) # Output: cherry
Explanation: In this example, we have a JSON string with an array of fruits. We parse it into a dictionary and then access elements in the array using indices.
Output:
1
2
apple
cherry
Example 7: Working with JSON Objects in a List
1
2
3
4
5
6
7
8
9
10
11
import json
# JSON data as a string with a list of objects
json_data = '[{"name": "Vaibhav Karad", "age": 30}, {"name": "Vaibhav karad", "age": 25}]'
# Parse JSON string to a Python list of dictionaries
data = json.loads(json_data)
# Accessing elements in the list of objects
print(data[0]['name']) # Output: Vaibhav Karad
print(data[1]['age']) # Output: 25
Explanation: Here, we have a JSON string with a list of objects representing people. We parse it into a list of dictionaries and then access elements within each dictionary.
Output:
1
2
Vaibhav Karad
25
Example 8: Handling JSON with Dates
1
2
3
4
5
6
7
8
9
10
import json
from datetime import datetime
# Python dictionary with a date
data = {'date': datetime(2023, 11, 1).isoformat()}
# Convert to JSON
json_data = json.dumps(data)
print(json_data) # Output: {"date": "2023-11-01T00:00:00"}
Explanation: In this example, we have a Python dictionary with a date (represented as a datetime object). We convert it to a JSON string using json.dumps().
Output:
1
{"date": "2023-11-01T00:00:00"}
Example 9: Error Handling for Invalid JSON
1
2
3
4
5
6
7
8
9
10
import json
# Invalid JSON data
invalid_json = '{"name": "Vaibhav Karad", "age": 30, "city": "New York"'
try:
data = json.loads(invalid_json)
print(data)
except json.JSONDecodeError as e:
print(f"Error: {e}")
Explanation: This example demonstrates error handling for cases where the provided string is not valid JSON. The json.loads() function can raise a JSONDecodeError, which we catch and print.
Output:
1
Error: Expecting ',' delimiter: line 1 column 35 (char 34)
Example 10: Reading and Writing from/to JSON Files
1
2
3
4
5
6
7
8
9
10
11
12
13
import json
# Writing to a JSON file
data_to_write = {'name': 'Vaibhav Karad', 'age': 30, 'city': 'New York'}
with open('output.json', 'w') as file:
json.dump(data_to_write, file)
# Reading from a JSON file
with open('output.json', 'r') as file:
data_read = json.load(file)
print(data_read) # Output: {'name': 'Vaibhav Karad', 'age': 30, 'city': 'New York'}
Explanation: In this example, we first write a Python dictionary to a JSON file using json.dump(). Then, we read the contents of the JSON file back into a Python dictionary using json.load().
Output: None (file created) / {'name': 'Vaibhav Karad', 'age': 30, 'city': 'New York'} (printed content)
These examples cover a range of scenarios for parsing and manipulating JSON data in Python. They demonstrate common operations like reading from files, handling nested structures, and working with both dictionaries and lists in JSON format.