Append to JSON file using Python

The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted string which contains the value in key-value mapping within { }.

Functions Used:

  • json.loads(): json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.

    Syntax: json.loads(json_string)



    Parameter: It takes JSON string as the parameter.

    Return type: It returns the python dictionary object.

  • json.dumps(): json.dumps() function is present in python built-in ‘json’ module. This function is used to covert Python object into JSON string.

    Syntax: json.dumps(object)

    Parameter: It takes Python Object as the parameter.

    Return type: It returns the JSON string.

  • update(): This method update the dictionary with elements from another dictionary object or from an iterable key/value pair.

    Syntax: dict.update([other])

    Parameters: Takes another dicitonary or an iterable key/value pair.

    Return type: Returns None.

Example 1: Updating a json string.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:


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

Example 2: Updating a JSON file. Suppose the json file looks like this.

python-json

We want to add another json data after emp_details. Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to update
# JSON
  
  
import json
  
  
# function to add to JSON
def write_json(data, filename='data.json'):
    with open(filename,'w') as f:
        json.dump(data, f, indent=4)
      
      
with open('data.json') as json_file:
    data = json.load(json_file)
      
    temp = data['emp_details']
  
    # python object to be appended
    y = {"emp_name":'Nikhil',
         "email": "nikhil@geeksforgeeks.org",
         "job_profile": "Full Time"
        }
  
  
    # appending data to emp_details 
    temp.append(y)
      
write_json(data) 

chevron_right


Output:

python-append-json



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.