Open In App

Modify Json Fields Using Python

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)

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 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)

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 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)

Output
Before Modifying: {"name": "Eva", "age": 28, "city": "Seattle"}
After Modifying: {"name": "Sophia", "age": 28, "city": "Seattle"}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads