Assuming you have a test.json file with the following content:

{"67790": {"1": {"kwh": 319.4}}}

Then, the code below will load the json file, update the data inside using dict.update() and dump into the test.json file:

import json

a_dict = {'new_key': 'new_value'}

with open('test.json') as f:
    data = json.load(f)

data.update(a_dict)

with open('test.json', 'w') as f:
    json.dump(data, f)

Then, in test.json, you'll have:

{"new_key": "new_value", "67790": {"1": {"kwh": 319.4}}}

Hope this is what you wanted.

Answer from alecxe on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Unable to append data to Json array object with desired output - Python Help - Discussions on Python.org
January 18, 2023 - I’m tried getting help for same issue on stack-overflow but got no help or replies. I’m re-posting here with the hope that someone can please guide me as I’m unable to push the code to repository due to delay. My code import json import re from http.client import responses import vt import requests with open('/home/asad/Downloads/ssh-log-parser/ok.txt', 'r') as file: file = file.read() pattern = re.compile(r'\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}') ips = pattern.findall(file) unique_ips = lis...
🌐
Python.org
discuss.python.org › python help
Appending JSON to same file - Python Help - Discussions on Python.org
January 5, 2023 - Hi, I need to make that within each request from api new dict will be appended to json. Now I have that each time data overwritten, but I need to append to existing file. How to achieve this? Code:(import requestsimpor…
🌐
HowToDoInJava
howtodoinjava.com › home › python json › python – append to json file
Python - Append to JSON File
December 9, 2022 - Learn to append JSON data into file in Python. To append, read the file to dict object, update the dict object and finally write the dict to the file.
Top answer
1 of 12
105

json might not be the best choice for on-disk formats; The trouble it has with appending data is a good example of why this might be. Specifically, json objects have a syntax that means the whole object must be read and parsed in order to understand any part of it.

Fortunately, there are lots of other options. A particularly simple one is CSV; which is supported well by python's standard library. The biggest downside is that it only works well for text; it requires additional action on the part of the programmer to convert the values to numbers or other formats, if needed.

Another option which does not have this limitation is to use a sqlite database, which also has built-in support in python. This would probably be a bigger departure from the code you already have, but it more naturally supports the 'modify a little bit' model you are apparently trying to build.

2 of 12
54

You probably want to use a JSON list instead of a dictionary as the toplevel element.

So, initialize the file with an empty list:

with open(DATA_FILENAME, mode='w', encoding='utf-8') as f:
    json.dump([], f)

Then, you can append new entries to this list:

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

Note that this will be slow to execute because you will rewrite the full contents of the file every time you call add. If you are calling it in a loop, consider adding all the feeds to a list in advance, then writing the list out in one go.

🌐
YouTube
youtube.com › watch
How to Append Data to a JSON File in Python? - YouTube
Full Tutorial: https://blog.finxter.com/how-to-append-data-to-a-json-file-in-python/Email Academy: https://blog.finxter.com/email-academy/►► Do you want to t...
Published   May 29, 2021
Find elsewhere
🌐
YouTube
youtube.com › watch
How to Append JSON files in Python - YouTube
If you enjoy this video, please subscribe. I provide all my content at no cost. If you want to support my channel, please donate viaPayPal: https://www.payp...
Published   February 12, 2020
🌐
Finxter
blog.finxter.com › home › learn python blog › how to append data to a json file in python? [+video]
How to Append Data to a JSON File in Python? [+Video] - Be on the Right Side of Change
June 1, 2022 - Problem Formulation Given a JSON object stored in a file named "your_file.json" such as a list of dictionaries. 💬 How to append data such as a new dictionary to it? # File "your_file.json" (BEFORE) [{"alice": 24, "bob": 27}] # New entry: {"carl": 33} # File "your_file.json" (AFTER) [{"alice": 24, "bob": 27}, {"carl": 33}] Method 1: ... Read more
🌐
CodeSpeedy
codespeedy.com › home › append to json file in python
Append to JSON file in Python - CodeSpeedy
July 3, 2020 - This tutorial will show you how to append to JSON file in Python using simple program. This program uses loads(), dumps(), update() methods.
🌐
Python Forum
python-forum.io › thread-26790.html
Append JSON's and write to file
I have a Sample JSON record in a file as below { 'name':'John', 'age':30, 'car':'BMW' } I want to create more JSON records out of it and i want to minimize the i/o by not writing to the file system everytime i create sample record, the code below is...
🌐
Medium
kendhia.medium.com › appending-a-json-item-to-a-json-list-cf3cf056481a
Appending a JSON item to a JSON list | by Dhia Kennouche | Medium
January 25, 2019 - I started recently working on a Python project, I loved the language. But as a person coming from Java World, the uncertainty (I call it like this) in the use of variables and sometimes the ambiguity in its errors is killing me. Of course probably this is happening ‘cuz I’m still a newbie in this beautiful world. Now to go to our point. I needed to open a file that contains a JSON List , and append a JSON object to it.
🌐
Python.org
discuss.python.org › python help
Json.dump does not append my data correctly - Python Help - Discussions on Python.org
February 14, 2024 - Separate, unrelated calls to json.dump won’t “combine” anything if you append to an existing file. You will need to read in the old JSON data, and append the new data in whatever way is appropriate in Python (e.g. in a list or something), then overwrite the old data file with a new call ...
🌐
Reddit
reddit.com › r/learnpython › how to append a json object to the beginning of the file?
r/learnpython on Reddit: How to append a json object to the beginning of the file?
November 4, 2021 -
def write(filename, new_data):  # function that appends data to the specified file
    with open(filename, "r") as file1:
        data = json.load(file1)
        data.append(new_data)
    with open(filename, "w") as file1:
        # Sets file's current position at offset.
        file1.seek(0)
        json.dump(data, file1, indent=4)

I have this function that appends objects exactly how I want; however, it only does it to the bottom of the file rather than the top.

🌐
Quora
quora.com › How-do-you-append-a-dictionary-to-a-JSON-file-Python
How to append a dictionary to a JSON file (Python) - Quora
Answer (1 of 3): [code]import json with open(r'Enter the full path name here ending with .json','w') as f: #The previous line will create the json file if doesn't exist Thing = {} Thing['stuff'] = [] Thing['stuff'].append({ 'Name' : 'John Doe', 'Age' : 35, 'Phone Number' : '123-4567...