typescriptadvanced
JSON Stream Parser
Parses large JSON arrays from a readable stream without loading the entire file into memory.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { createReadStream } from 'fs';
import { Transform, TransformCallback } from 'stream';
class JsonLineTransform extends Transform {
private buffer = '';
_transform(chunk: Buffer, _encoding: string, callback: TransformCallback) {
this.buffer += chunk.toString();
const lines = this.buffer.split('\n');
this.buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (trimmed) {
try {
this.push(JSON.parse(trimmed));
} catch { /* skip malformed lines */ }
}
}
callback();
}
_flush(callback: TransformCallback) {
if (this.buffer.trim()) {
try {
this.push(JSON.parse(this.buffer.trim()));
} catch { /* skip */ }
}
callback();
}
}
async function* parseJsonLines(filePath: string) {
const stream = createReadStream(filePath).pipe(new JsonLineTransform({ objectMode: true }));
for await (const item of stream) {
yield item;
}
}
export { parseJsonLines };Use Cases
- Processing large log files
- ETL pipelines
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Multipart Upload Stream Handler
Handles multipart file uploads with streaming to disk without buffering the entire file in memory.
Best for: Large file uploads
#upload#streaming
typescriptadvanced
Partial Prerendering with Suspense
Combine static shells with streamed dynamic content using React Suspense for instant page loads.
Best for: E-commerce product pages
#ppr#suspense
typescriptintermediate
Next.js Streaming with Suspense
Stream server components with Suspense boundaries for progressive page loading and better TTFB.
Best for: Progressive loading for data-heavy dashboards
#nextjs#streaming
pythonintermediate
Generator Pipeline for Data Processing
Chain generators to build memory-efficient data processing pipelines for large files and streams.
Best for: Large file ETL
#generator#pipeline