Modify Json Fields Using Python - GeeksforGeeks (2024)

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"}


Please Login to comment...

Modify Json Fields Using Python - GeeksforGeeks (2024)
Top Articles
Dividend uitkeren - hoe en wanneer is het mogelijk? + [Video]
What Happens If You Have No Insurance But The Other Driver Was At Fault? — Crockett Law Group
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 6136

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.