How to update JSON in Python

JSON is javascript object notation, python supports JSON using the package named JSON. JSON is similar to the dictionary datatype of python with more functionality in it. This tutorial, will how we can update the dynamic data in a JSON file using python.

The data types we use in python are quite different from those of JSON like


JSON module enables us to use the functionality to use javascript object as a tool of scripting

Two basic methods:

json.dump() = to insert data into the json file

json.load() = to read the data from the json file

There are many other methods that make json very much flexible

Algorithm of the code:

  • import module
  • opening json file
  • using iteration to insert data one by one
  • closing the file

Source code of the program:

import json

name = input('enter name')
phonenumber = int(input("enter phone number"))
age = int(input('enter age'))

dictionary = {
    'employ': {

        "name": name,
        "phonenumber": phonenumber,
        'age': age

with open("sample.json", "a") as outfile:
    json.dump(dictionary, outfile, indent=4)

Explanation of the code:-

  1. importing the module required i.e. json
  2. creating input of the data like name age and phone number to add general data in the json file
  3. creating a dictionary which we will convert into the json object
  4.  using the open function we open file by giving filename and mode 
  5. dumping the dictionary using dump() we created into json file with indent parameter we make the json object more attractive to view and understand

Inserting the data 

Overview of sample.json


“In this module, we learn how to add data dynamically in json file using python”