Python Forum
How to optimize the speed of processing large JSON files in Python without using too
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to optimize the speed of processing large JSON files in Python without using too
#1
Hello everyone, I am processing very large JSON files (GB) in Python, but my RAM is limited, so I often run out of memory. Can everyone suggest a way to read/write and process large JSON in the most RAM-saving way? Should I use any library other than the default JSON? Thanks, everyone, in advance! Monkey Mart
[link removed]
Reply
#2
A json file is just a text file.

What are you doing to have GB size text files??

Have a look at this link for some tips.
Reply
#3
never tried this for a while, but maybe can you use pandas (to be check):
df = pd.read_json(jsonFile)
Reply
#4
Pandas as mention is ok,it has chunksize build in.
import pandas as pd

reader = pd.read_json("big.json", lines=True, chunksize=100_000)
for chunk in reader:
    filtered = chunk[chunk['value'] > 100]
    # further processing
Using Polars would be better.
import polars as pl

# Lazy scan of newline-delimited JSON
# builds(stream) a query plan without immediately reading all rows
df = pl.scan_json("big.json")  

# Example: filter and select
result = (
    df.filter(pl.col("value") > 100)
      .select(["id", "value", "timestamp"])
      .with_column(pl.col("value") * 2)
)

# Collect into memory only the reduced result
df_small = result.collect()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  image processing with opencv-python and Tesseract OCR marchellopl 4 157 Feb-15-2026, 12:27 AM
Last Post: Pedroski55
  backtesting can't optimize Running_Code 1 1,001 May-23-2025, 07:46 PM
Last Post: snippsat
  how to download large files faster? kucingkembar 3 2,354 Feb-20-2025, 06:57 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
  speed up getting embedding from bert model for large set of text veda 7 3,491 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Trying to generating multiple json files using python script dzgn989 4 4,664 May-10-2024, 03:09 PM
Last Post: deanhystad
  Parsing large JSON josvink66 5 3,189 Jan-10-2024, 05:46 PM
Last Post: snippsat
  Processing Files that are not in use randywberry 3 3,113 Jun-06-2023, 06:00 PM
Last Post: rajeshgk
  validate large json file with millions of records in batches herobpv 3 3,066 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Merge all json files in folder after filtering deneme2 10 5,846 Sep-18-2022, 10:32 AM
Last Post: deneme2

Forum Jump:

User Panel Messages

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