Package Exports
- ndjson-parser
- ndjson-parser/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ndjson-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
NDJSON Parser
A simple NDJSON (Newline Delimited JSON) parser for Bun, Deno, and Web environments. It provides functions to parse NDJSON strings, streams, and a transformer for real-time processing using TransformStream.
Features
- String Parsing: Synchronously parse an NDJSON string into an array of objects.
- Stream Parsing: Read a
ReadableStream(e.g., fromfetch) and return a promise resolving to an array of objects. - TransformStream Support: Use a dedicated
TransformStreamto process NDJSON chunks as they arrive. - Zero Dependencies: Uses native
TextDecoderand Web Stream APIs.
Installation
bun add ndjson-parser
# or
npm install ndjson-parserUsage
1. Parse an NDJSON String
import ndjson from 'ndjson-parser';
const data = `{"id": 1, "name": "foo"}
{"id": 2, "name": "bar"}`;
const result = ndjson.parse(data);
console.log(result);
// Output: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]2. Parse a ReadableStream
import ndjson from 'ndjson-parser';
const response = await fetch('https://example.com/data.ndjson');
const result = await ndjson.parseStream(response.body);
console.log(result);3. Use with TransformStream (Real-time)
import ndjson from 'ndjson-parser';
const response = await fetch('https://example.com/data.ndjson');
const transformer = ndjson.createTransformer();
const reader = response.body
.pipeThrough(transformer)
.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
console.log('Received object:', value);
}API
parse(ndJsonString)
Synchronously parses an NDJSON string. Returns an array of objects.
parseStream(readableStream)
Reads a ReadableStream<Uint8Array> and returns a Promise that resolves to an array of parsed objects.
createTransformer()
Returns a TransformStream<Uint8Array, any> that transforms chunks of NDJSON data into parsed JavaScript objects.
License
MIT