Python Forum
Initializing, reading and updating a large JSON file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Initializing, reading and updating a large JSON file
#1
Hello,
I want to work with a large JSON file in the following form:

{"1": {"data": "abc"},"2": {"data": "xyz"},...}
I want be continually fetching data from my source and adding the data in the above indexed format. Each data block is around 60kbytes, and the index is expected to grow to a million or more, so I will be handling several Gigabytes of data that might not potentially fit into memory.

So basically what I want to do when my code runs is to open the json data file (e.g. "out.json"), find the greatest index (e.g. 120000) and continue appending from there.

I am not familiar though with handling JSON files in python, let alone large ones. I tried to do something with the code below. However I don't want to store the whole thing into memory, and I want to incrementally write into the file.

Any help appreciated.

import json
import random


with open("out.json", "r+") as file:

    jsondata = json.load(file)

    #Find largest index??

    for i in range(largest_index+1,1000000):
        
        #continue in next index
        jsondata[str(i)]={"data":random.random()}
    file.seek(0)
    json.dump(jsondata, file)
    file.truncate()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors using json file RonR 5 955 Oct-28-2025, 03:00 PM
Last Post: buran
  How to optimize the speed of processing large JSON files in Python without using too sophia2005 3 963 Aug-02-2025, 03:25 PM
Last Post: snippsat
  Problems writing a large text file in python Vilius 4 2,033 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Reading an ASCII text file and parsing data... oradba4u 2 2,897 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  encrypt data in json file help jacksfrustration 1 3,568 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 3,949 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 15,077 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  Parsing large JSON josvink66 5 3,188 Jan-10-2024, 05:46 PM
Last Post: snippsat
Sad problems with reading csv file. MassiJames 3 4,070 Nov-16-2023, 03:41 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020