Modify Json Fields Using Python
Last Updated :
15 Mar, 2024
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
Modify Json Fields Using Python
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 data
json_data = '{"name": "Alice", "age": 30, "city": "Los Angeles"}'
# Parse the JSON data
data = json.loads(json_data)
# Specify the field key to update
field_key = 'age'
# Update the specified field value
if field_key in data:
data[field_key] += 1
# Convert the modified data back to JSON
modified_json = json.dumps(data)
print('Before Modifying:', json_data)
print('After Modifying:', modified_json)
OutputBefore 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 data
json_data = '{"name": "John", "age": 25, "city": "New York"}'
data = json.loads(json_data)
# Modify the 'age' field
data['age'] = 26
# Convert the modified data back to JSON
modified_json = json.dumps(data)
print('Before Modifying:', json_data)
print('After Modifying:', modified_json)
OutputBefore 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 data
json_data = '{"name": "Eva", "age": 28, "city": "Seattle"}'
# Parse the JSON data and convert it to a dictionary
data = json.loads(json_data)
# Modify the 'name' field
data.update({"name": "Sophia"})
# Convert the modified data back to JSON
modified_json = json.dumps(data)
print('Before Modifying:', json_data)
print('After Modifying:', modified_json)
OutputBefore Modifying: {"name": "Eva", "age": 28, "city": "Seattle"}
After Modifying: {"name": "Sophia", "age": 28, "city": "Seattle"}
Similar Reads
Python - Modify Strings Python provides an wide range of built-in methods that make string manipulation simple and efficient. In this article, we'll explore several techniques for modifying strings in Python.Start with doing a simple string modification by changing the its case:Changing CaseOne of the simplest ways to modi
3 min read
Convert CSV to JSON using Python Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
2 min read
Convert Bytes To Json using Python When dealing with complex byte data in Python, converting it to JSON format is a common task. In this article, we will explore different approaches, each demonstrating how to handle complex byte input and showcasing the resulting JSON output. Convert Bytes To JSON in PythonBelow are some of the ways
2 min read
Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read
In-Place String Modifications in Python Strings are immutable in Python, meaning their values cannot be modified directly after creation. However, we can simulate in-place string modifications using techniques such as string slicing and conversion to mutable data types like lists.Using String SlicingString slicing is one of the most effic
2 min read