Streaming JSON
For large JSON files, streaming parsers process data incrementally without loading the entire file into memory.
For large JSON files, streaming parsers process data incrementally without loading the entire file into memory.
// Using the Streams API + TextDecoder for NDJSON
const response = await fetch("https://api.example.com/stream");
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const lines = decoder.decode(value).split("
");
for (const line of lines) {
if (line) {
const item = JSON.parse(line); // Newline-delimited JSON
processItem(item);
}
}
}
Newline-delimited JSON (NDJSON) is a simple format for streaming — one JSON object per line.
More in JavaScript