How to split a large JSON file into multiple files
· 5 min read · By Amir Badi
A file can be too big for the next step even when nothing is wrong with it: an upload form that stops at 10 MB, an import that times out, a test that only needs a thousand records. Splitting it means cutting the list inside it into parts that each stay valid JSON. This guide does it by record count, by file size and by a field's value, with jq, Python and in the browser.
I ran everything below with jq 1.7, GNU coreutils 9.4 and Python 3.12 on orders.json: an array of 2,500 orders, 223 KB indented.
[
{ "id": 1, "status": "paid", "country": "FR", "total": 474.19 },
{ "id": 2, "status": "pending", "country": "FR", "total": 327.21 },
...
]
Find the list first
You don't split a file, you split one list in it. If the file is an array, that's the list. If it's an object, like an API response or an export with metadata, the records sit one level down:
{ "meta": { "count": 2500 }, "orders": [ … ] }
In the jq commands below, write .orders[] instead of .[] for a file like this. The meta part is then left out of the parts; decide whether each part needs a copy of it.
Equal parts with jq
The quickest split writes one record per line and lets split cut the lines:
jq -c '.[]' orders.json | split -l 1000 -d --additional-suffix=.jsonl - orders-
1000 orders-00.jsonl
1000 orders-01.jsonl
500 orders-02.jsonl
Each part is JSON Lines (one compact record per line), which most importers, databases and data tools read directly. -d numbers the parts 00, 01, 02 so they sort in order. This is GNU split, the one on Linux; on a Mac, install coreutils and run gsplit.
If the next program wants a JSON array in each file, have jq cut the array into slices and write one slice per line:
jq -c '. as $all | range(0; length; 1000) | $all[.:. + 1000]' orders.json \
| split -l 1 -d --additional-suffix=.json - part-
part-00.json 1000 records, ids 1 to 1000
part-01.json 1000 records, ids 1001 to 2000
part-02.json 500 records, ids 2001 to 2500
Over 100 parts, add -a 3 so the numbers get three digits.
By a field's value
To get one file per status, country or customer, list the values and select each one:
for s in $(jq -r 'map(.status) | unique[]' orders.json); do
jq --arg s "$s" 'map(select(.status == $s))' orders.json > "orders-$s.json"
done
orders-paid.json 834 records
orders-pending.json 833 records
orders-shipped.json 833 records
This reads the file once per value, which is fine for a handful of values and slow for hundreds. It also breaks on values with spaces, since the shell splits them into words. Records without the field end up in no file at all: count them with jq 'map(select(.status == null)) | length' orders.json first.
By file size, in Python
A size limit is the case jq handles badly, because you only know a part's size once you've written it. Python can count as it goes:
import json
MAX_BYTES = 50_000 # each part stays at or under this size
with open("orders.json", encoding="utf-8") as f:
records = json.load(f)
parts, current, size = [], [], 2 # 2 bytes for the [ and ]
for rec in records:
text = json.dumps(rec, ensure_ascii=False, separators=(",", ":"))
extra = len(text.encode("utf-8")) + (1 if current else 0) # +1 for the comma
if current and size + extra > MAX_BYTES:
parts.append(current)
current, size = [], 2
extra -= 1
current.append(text)
size += extra
if current:
parts.append(current)
for n, part in enumerate(parts, 1):
with open(f"orders-{n:03}.json", "w", encoding="utf-8") as out:
out.write("[" + ",".join(part) + "]")
print(f"orders-{n:03}.json", len(part), "records")
orders-001.json 854 records 49,999 bytes
orders-002.json 840 records 49,992 bytes
orders-003.json 806 records 48,119 bytes
Three things make the limit hold. Sizes are counted in UTF-8 bytes, not characters, since an é takes two bytes on disk. The brackets and commas count too. And the parts are compact: the same 2,500 orders take 148 KB minified against 223 KB indented, so indenting the parts would push each one over. A single record bigger than the limit still gets a file of its own.
Files too big to load
json.load and plain jq read the whole file into memory before splitting, which takes several times the file's size. For a file of several gigabytes, stream it. jq's --stream mode emits each top-level record as soon as it's complete:
jq -cn --stream 'fromstream(1|truncate_stream(inputs))' big.json \
| split -l 100000 -d --additional-suffix=.jsonl - big-
It's slower than normal jq, but memory stays flat. For a list inside an object, keep only the events under that key and cut two levels:
jq -cn --stream 'fromstream(2|truncate_stream(inputs | select(.[0][0] == "orders")))' export.json
In Python, the ijson package reads records one at a time (ijson.items(f, "item") for a top-level array, "orders.item" for the list under orders), and you can write parts as you go. How to open a large JSON file covers both tools.
In the browser
GigaJSON's JSON Splitter does all of the above without a terminal. Open a JSON or JSON Lines file of up to 1 GB; it's read on your computer, not uploaded. It finds the lists in the file, down to three levels, and picks the longest. Then you choose how to cut:
- Records per file or number of files
- Maximum size, counted in UTF-8 bytes so no part goes over
- By field value: one file per value, with records missing the field in a file of their own or left out
- Top-level keys: one file per key of an object
Each part can be a JSON array, JSON Lines, or the same shape as the input (so meta comes along in every part), minified or indented, with only the fields you keep. The preview lists every part with its record count and size before anything is written, and Split downloads them in one .zip, or saves them to a folder in Chrome and Edge. It can also take a sample instead: the first or last N records, a random set with a seed, every Nth record or a percentage.
Join the parts back
Name the parts with zero-padded numbers and the shell sorts them correctly. Then:
jq -s 'add' part-*.json > all.json # JSON arrays: one array again
cat orders-*.jsonl > all.jsonl # JSON Lines: just concatenate
Both give the 2,500 records back in their original order. To turn the JSON Lines parts into one array, use jq -s '.' or JSON Lines to JSON; Convert JSON to JSON Lines and back covers both directions.