Movatterモバイル変換


[0]ホーム

URL:


Open In App

JSON (JavaScript Object Notation) is a lightweight data format that stores data as key-value pairs within curly braces {}. Python's json module makes it easy to work with JSON data, whether you are parsing JSON strings, converting Python objects to JSON, or appending new data to existing JSON files.

Key Functions injson Module: 

1. json.loads()

Parses a JSON string and returns a Python dictionary.

Syntax: json.loads(json_string)
Parameter:It takes JSON string as the parameter.
Return type:It returns the python dictionary object. 

2. json.dumps()

Converts a Python object to a JSON string.

Syntax: json.dumps(object)
Parameter:It takes Python Object as the parameter.
Return type:It returns the JSON string. 

3. update()

Updates a dictionary with elements from another dictionary or iterable key-value pairs.

Syntax: dict.update([other])
Parameters: Takes another dictionary or an iterable key/value pair.
Return type: Returns None. 

Example 1: Updating a JSON string.

Python
importjsonx='{ "organization":"GeeksForGeeks","city":"Noida","country":"India"}'# python object to be appendedy={"pin":110096}# parsing JSON string:z=json.loads(x)# appending the dataz.update(y)# the result is a JSON string:print(json.dumps(z))

Output:

{"organization": "GeeksForGeeks", "city": "Noida", "country": "India", "pin": 110096}

Example 2: Updating a JSON file.

Thewrite_json() function reads the existing data, updates it, and then writes the updated data back to the file. Suppose the JSON file looks like this.

python-json

We want to add another JSON data afteremp_details. Below is the implementation.

Python
importjson# Function to append new data to JSON filedefwrite_json(new_data,filename='data.json'):withopen(filename,'r+')asfile:# Load existing data into a dictionaryfile_data=json.load(file)# Append new data to the 'emp_details' listfile_data["emp_details"].append(new_data)# Move the cursor to the beginning of the filefile.seek(0)# Write the updated data back to the filejson.dump(file_data,file,indent=4)# New data to appendnew_employee={"emp_name":"Nikhil","email":"nikhil@geeksforgeeks.org","job_profile":"Full Time"}# Call the function to append datawrite_json(new_employee)

Output:

python-append-json

Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp