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
  Problems writing a large text file in python Vilius 4 1,057 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,094 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Reading an ASCII text file and parsing data... oradba4u 2 1,471 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  encrypt data in json file help jacksfrustration 1 2,285 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 2,110 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 11,004 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  Parsing large JSON josvink66 5 2,100 Jan-10-2024, 05:46 PM
Last Post: snippsat
Sad problems with reading csv file. MassiJames 3 2,632 Nov-16-2023, 03:41 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 2,065 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Updating sharepoint excel file odd results cubangt 1 2,092 Nov-03-2023, 05:13 PM
Last Post: noisefloor

Forum Jump:

User Panel Messages

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