Python JSON Append to JSON file using Python [Updating a json string.]














































Python JSON Append to JSON file using Python [Updating a json string.]



Append to JSON file using Python  [Updating a json string.]


 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 convert 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.

python code:
# Python program to update 
# JSON 


import json 

# JSON data: 
x  = '{"name": "Bob", "languages": "English"}'
# 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)) 

output:

{"name": "Bob", "languages": "English", "pin": 110096}








Comments