Last Updated : 15 Mar, 2024
Summarize
Comments
Improve
We have a task to modify JSON fields using Python and print the result. In this article, we will see some generally used methods for modifying JSON fields using Python.
Example:
Input : '{"name": "Alice", "age": 30, "city": "Los Angeles"}' ,
age : 31
Output : '{"name": "Alice", "age": 31, "city": "Los Angeles"}'
Explanation : Here, we modify the age 30 to 31 using age intialization
Below are the methods by which we can Modify JSON fields Using Python:
- Using JSON Field Value
- Using
json
Library - Using Update() Method
Modify JSON Fields Using JSON Field Value
In this example, in the below code, the `json` library parses a sample JSON data string, increments the value of the ‘age’ field by 1, and then converts the modified data back to JSON format. The resulting JSON, with the updated age value, is printed to the console.
Python3 import json# Sample JSON datajson_data = '{"name": "Alice", "age": 30, "city": "Los Angeles"}'# Parse the JSON datadata = json.loads(json_data)# Specify the field key to updatefield_key = 'age'# Update the specified field valueif field_key in data: data[field_key] += 1# Convert the modified data back to JSONmodified_json = json.dumps(data)print('Before Modifying:', json_data)print('After Modifying:', modified_json)
Output
Before Modifying: {"name": "Alice", "age": 30, "city": "Los Angeles"}After Modifying: {"name": "Alice", "age": 31, "city": "Los Angeles"}
Modify JSON Fields Using Json Library
In this example, below code parses a sample JSON data string representing a person’s information, updates the ‘age’ field from 25 to 26, and then converts the modified data back to JSON format. The resulting JSON with the incremented age is printed to the console.
Python3 import json# Sample JSON datajson_data = '{"name": "John", "age": 25, "city": "New York"}'data = json.loads(json_data)# Modify the 'age' fielddata['age'] = 26# Convert the modified data back to JSONmodified_json = json.dumps(data)print('Before Modifying:', json_data)print('After Modifying:', modified_json)
Output
Before Modifying: {"name": "John", "age": 25, "city": "New York"}After Modifying: {"name": "John", "age": 26, "city": "New York"}
Modify JSON Fields Using update() Method
In this example, below code parses a sample JSON data string representing a person’s information, changes the ‘name‘ field from “Eva” to “Sophia,” and then converts the modified data back to JSON format. The resulting JSON, with the updated name, is printed to the console.
Python3 import json# Sample JSON datajson_data = '{"name": "Eva", "age": 28, "city": "Seattle"}'# Parse the JSON data and convert it to a dictionarydata = json.loads(json_data)# Modify the 'name' fielddata.update({"name": "Sophia"})# Convert the modified data back to JSONmodified_json = json.dumps(data)print('Before Modifying:', json_data)print('After Modifying:', modified_json)
Output
Before Modifying: {"name": "Eva", "age": 28, "city": "Seattle"}After Modifying: {"name": "Sophia", "age": 28, "city": "Seattle"}