A JSON file of a few megabytes is nothing for a modern computer. Problems start in the hundreds of megabytes, or when a large response blocks a web page while it's parsed. Here's what to do at each size.
Up to tens of megabytes: just parse it
JSON.parse in browsers and Node.js, and json.loads in Python, are fast native code. Parsing a 20 MB file typically takes a fraction of a second. The cost is memory: the parsed objects take several times the file size. Measure before optimising:
const t0 = performance.now();
const data = JSON.parse(text);
console.log(`parsed in ${Math.round(performance.now() - t0)} ms`); In the browser: keep the page responsive
Parsing blocks the main thread, so a big response can freeze the page. Move the parsing into a Web Worker:
// worker.js
self.onmessage = e => self.postMessage(JSON.parse(e.data));
// main.js
const worker = new Worker('worker.js');
worker.onmessage = e => render(e.data);
worker.postMessage(await response.text()); Passing the result back copies it, which also costs time, so send back only what the page needs (for example the first 1,000 rows or a summary), not the whole object.
Hundreds of megabytes: stream it
Streaming parsers read the file piece by piece and hand you one item at a time, so memory stays flat:
// Node.js, with the stream-json package
const { parser } = require('stream-json');
const { streamArray } = require('stream-json/streamers/StreamArray');
fs.createReadStream('orders.json')
.pipe(parser())
.pipe(streamArray())
.on('data', ({ value }) => handle(value)); # Python, with the ijson package
import ijson
with open("orders.json", "rb") as f:
for order in ijson.items(f, "item"):
handle(order) A reviver function passed to JSON.parse doesn't save memory: the whole text is still parsed first.
Better: don't produce one giant document
- Paginate APIs so clients ask for 100 or 1,000 records at a time.
- Use NDJSON (one JSON object per line) for exports and logs. Every line parses on its own, files can be split and appended, and tools like
jqhandle them line by line. - Compress on the wire. JSON compresses very well with gzip or Brotli. Enable it on the server; browsers request compressed responses automatically.
- Minify stored JSON; indentation can add a large share of the size. The JSON minifier shows the saving.
Just need to look inside?
To inspect a big file without writing code, open it in the JSON viewer, which shows a collapsible tree and lets you search keys and values, or query it with SQL in SQL on CSV & JSON. Both run in your browser, so the file isn't uploaded.
Try it: JSON Viewer · JSON Minifier · SQL on JSON & CSV. Free, no sign-up.