JSPM

ndjson-parser

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 8
    • Score
      100M100P100Q41269F
    • License MIT

    A simple NDJSON (Newline Delimited JSON) parser for Bun and Web environments using TransformStream and TextDecoder.

    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., from fetch) and return a promise resolving to an array of objects.
    • TransformStream Support: Use a dedicated TransformStream to process NDJSON chunks as they arrive.
    • Zero Dependencies: Uses native TextDecoder and Web Stream APIs.

    Installation

    bun add ndjson-parser
    # or
    npm install ndjson-parser

    Usage

    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