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
  encrypt data in json file help jacksfrustration 1 224 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 339 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 7,020 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  Parsing large JSON josvink66 5 668 Jan-10-2024, 05:46 PM
Last Post: snippsat
Sad problems with reading csv file. MassiJames 3 644 Nov-16-2023, 03:41 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 764 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Updating sharepoint excel file odd results cubangt 1 851 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Reading a file name fron a folder on my desktop Fiona 4 920 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  TypeRoor reading json GreenLynx 3 868 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,535 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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