DEV Community

Puneet Khandelwal
Puneet Khandelwal

Posted on

Parsing Four Gigabytes of Legacy Municipal JSON Without OOMing

When a municipal server returns a four-gigabyte JSON payload in a single, unpaginated HTTP response, standard methods break down. Most backend engineers reach for a built-in JSON deserializer first. They load the entire string into memory, parse the document into a massive object tree, and watch the process container crash with an out of memory error. We hit this exact wall last week while ingesting real-time public transit logs from a legacy city transit API. The endpoint lacks pagination parameters, and the data arrives as one giant array of nested objects containing vehicle telemetry, route changes, and maintenance alerts.

The problem isn't the size of the data itself. Four gigabytes fits comfortably on a modern server disk or even in RAM. The failure stems from how standard deserializers attempt to map that text into memory. Object allocation overhead in languages like JavaScript, Python, or Ruby easily multiplies raw JSON size by a factor of five or more. A four-gigabyte string transforms into a twenty-gigabyte object graph, instantly exhausting available system resources.

To survive this ingestion task, we had to abandon DOM style parsers in favor of token-based streaming. Instead of loading the whole payload, a streaming parser reads the HTTP response stream chunk by chunk. It fires events or yields control when it detects structural boundaries like the start of an array element, the end of a key, or a closing bracket. This design keeps the active memory footprint nearly constant regardless of whether the input file is four megabytes or four terabytes.

Here is a conceptual pattern using Node.js and a streaming transformation approach to filter and process large civic records without retaining the whole dataset:

const { chain } = require('stream-chain');
const { parser } = require('stream-json');
const { pick } = require('stream-json/filters/Pick');
const { streamArray } = require('stream-json/streamers/StreamArray');
const fs = require('fs');

const pipeline = chain([
 fs.createReadStream('transit_logs.json'),
 parser(),
 pick({ filter: 'data' }),
 streamArray()
]);

pipeline.on('data', ({ key, value }) => {
 if (value.status === 'delayed') {
 // Process single record immediately without holding sibling objects
 saveToDatabase(value);
 }
});

pipeline.on('end', () => {
 console.log('Finished streaming and parsing municipal feed.');
});
Enter fullscreen mode Exit fullscreen mode

This architecture shifts the operational model from batch processing to event-driven pipelining. Records are read, evaluated, written to a persistent datastore, and garbage collected within milliseconds. The memory profile remains flat, hovering at just a few dozen megabytes throughout the entire run.

Building resilient civic infrastructure requires acknowledging that public data sources are rarely pristine. Legacy systems will continue to serve monolithic payloads, and engineers must design pipelines that respect physical memory limits. When we stop trying to hold the whole world in RAM, our systems finally gain the stability that communities depend on.

Top comments (0)