Parsing large JSON files in Python with ijson

By luna (@luna.pds.witchcraft.systems)
Published:

import ijson

# assuming your json is like: [{"id": 1, ...}, {"id": 2, ...}]
# and you want to process each object in the top-level array

with open('your_huge_file.json', 'rb') as f:
    # The prefix 'item' tells ijson to look for items in the top-level array.
    # If your JSON is structured differently, you might need to change this.
    # For example, if your array is under a key called "records", you'd use "records.item".
    for record in ijson.items(f, 'item'):
        # now you can process each record one by one without loading the whole file!
        # for example, you could print the id of each record:
        # print(record.get('id'))
        process_record(record)

def process_record(record):
    # do something with the record here
    print(f"processing record: {record.get('id')}")