Skip to content

Append to JSON file Python

  • by

Use json.loads() function to Append to JSON file in Python. But for JSON files you can append new entries to this list and convert them back to JSON.

with open(DATA_FILENAME, mode='w', encoding='utf-8') as feedsjson:
    entry = {'name':, 'url': args.url}
    json.dump(feeds, feedsjson)

Append to JSON file Python

Simple example code.

import json

def write_json(new_data, filename='data.json'):
with open(filename, 'r+') as file:
file_data = list(json.load(file))
# file_data["emp_details"].append(new_data)
json.dump(file_data, file, indent=4)

z = {'pin': 11001}



Append to JSON file Python

Updating a JSON string

import json

data = '{ "ORG":"EyeHunts","city":"BLR","C":"India"}'

# python object to be appended
add = {"pin": 56001}

# parsing JSON string:
res = json.loads(data)

# appending the data

# the result is a JSON string:

Output: {“ORG”: “EyeHunts”, “city”: “BLR”, “C”: “India”, “pin”: 56001}

How to check if JSON is empty, If not, append new data

Answer: First check whether a JSON file is empty or not then allow append any content and then write them back. Make sure that the file itself exists, otherwise ‘FileNotFoundError’ will occur.

import json
with open("fileName.json", "r+") as f:
    json_data = json.load(f)
    json.dump(json_data, f)
  except ValueError:
    print("Empty File!")

Do comment if you have any doubts or suggestions so on this Python file topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.