How to open a large JSON file without a freeze
Why most viewers die at 20 MB, what to do instead, and how to read a file of several hundred megabytes.
You have a file of 200 MB. Your editor shows a spinning wheel. The online
viewer makes the tab stop answering. cat fills the terminal with one endless
line. This page explains why, and gives you the way out.
Why the usual tools fail
The memory problem
A JSON parser reads text and builds objects. Those objects are far bigger than the text they came from.
The number 42 is two characters in the file. In a JavaScript engine it
becomes a value with a type tag, a reference and padding — about 16 bytes.
A short key of five characters becomes a string object of about 40 bytes.
The rule of thumb is eight to twelve times the file size. A file of 200 MB needs about 2 GB of memory, and a browser tab usually has less than that.
The element problem
The second failure is worse, and it belongs to web viewers. They build one element in the page for each value in the file. A file of 200 MB holds about 16 million values. A browser handles a few tens of thousands of elements comfortably. Sixteen million is three orders of magnitude too many.
This is why a web viewer that works beautifully at 2 MB stops dead at 20 MB. The failure is sudden, not gradual.
The one-line problem
Many large files hold no line breaks at all. An editor measures the width of a line to draw a scrollbar. A line of 200 million characters takes a long time to measure, and the editor measures it again every time the window changes size.
The answers, in order of effort
1. Open it in this viewer
This tool was built for the problem. It reads the text one time and writes a
table of numbers: where every value starts, where it ends, and which value
holds it. It never calls JSON.parse, and it never builds an object for a
value.
The tree shows only the rows you can see, about sixty of them, whatever the size of the file.
Measured on a real file of 120 MB with 9.6 million values:
| Work | Time |
|---|---|
| Read the file | 1.3 seconds |
| Open every branch | 0.3 seconds |
| Draw one screen while scrolling | 0.4 milliseconds |
| Run a JSONPath query | 10 milliseconds |
| Find a word everywhere | 0.6 seconds |
| Format the whole file | 2.2 seconds |
The memory is about 690 MB for that file, against the 1.2 GB or more an object graph would need.
What to do first: open the file, press Collapse all, then open one level at a time. The shape of the file appears in a few seconds. Turn on Show size in the options to see which branch holds the bytes. That answers "why is this file so big?" faster than anything else.
2. Cut it down first, on the command line
If you only need a part of the file, jq streams it and never holds it all:
# the first 100 records
jq -c '.records[:100]' big.json > small.json
# only the fields you need
jq -c '[.records[] | {id, email}]' big.json > small.json
# count without loading
jq '.records | length' big.json
The --stream option goes further and reads the file in pieces:
jq --stream -c 'select(length==2)' big.json | head -50
3. Look at the start
Sometimes you only need to see the shape:
head -c 2000 big.json | python3 -m json.tool
4. Change the format
If you control the program that writes the file, two changes fix the problem for good.
Write JSON Lines. One record on each line. Every tool can then read one
line at a time, and grep, head, wc -l and split all work. See
JSON Lines.
Page the data. A list of a million records is almost never useful as one file. Give the reader a page at a time.
5. Read it in a program
In Python, ijson reads a file in pieces:
import ijson
with open('big.json', 'rb') as f:
for record in ijson.items(f, 'records.item'):
process(record)
Memory stays flat, whatever the size of the file.
In Node, use a streaming parser such as stream-json:
import { parser } from 'stream-json'
import { streamArray } from 'stream-json/streamers/StreamArray'
fs.createReadStream('big.json')
.pipe(parser())
.pipe(streamArray())
.on('data', ({ value }) => process(value))
Which way to choose
| Your need | The way |
|---|---|
| Look at it, understand it, search it | This viewer |
| Take a small part out | jq |
| Run it in a program, once | ijson or stream-json |
| Make the problem go away | Write JSON Lines, or page the data |
What not to do
- Do not open it in a plain text editor. It will measure that one long line for a long time.
- Do not use
JSON.parseon it in a browser tab. The tab has a memory limit and you will meet it. - Do not paste it into a viewer that uploads it. Many online tools send your file to a server. If the data is private, that is a leak. This tool never sends it, and the privacy page says exactly what happens.
One more thing: the big number
A large file often holds identifiers with 18 or more digits. Most tools read them into a floating point number and quietly change the last digits. Read big numbers in JSON before you trust a tool with such a file. This viewer keeps the digits exactly, because it never turns the text into a number.
Try it on your own file. The tool runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open the JSON ViewerRead next
JSON Lines (NDJSON): what it is and when to use it
One JSON value a line. Why logs and data exports use it, and how to read, write and convert it.
How to compare two JSON files
Why a plain text diff fails on JSON, what a real JSON comparison does, and how to read the result.