Incomplete JSON Parser

1.1.5 · active · verified Tue Apr 21

incomplete-json-parser is a TypeScript module designed to robustly parse JSON strings that may be incomplete, chunked, or arriving in a streaming fashion. It provides an `IncompleteJsonParser` class and a static `parse` method to handle scenarios where standard `JSON.parse` would fail due to premature termination or fragmented input. The library, currently at version `1.1.5`, allows developers to incrementally feed JSON data via a `write` method and retrieve the most complete possible JavaScript object at any point using `getObjects()`. This makes it particularly suitable for processing real-time data streams, large file parsing, or handling responses from APIs that might send data in chunks or terminate early (e.g., during AI model responses). While a precise release cadence isn't defined, its versioning indicates active development and minor updates. A key differentiator is its fault tolerance, aiming to yield valid partial results even from highly fragmented inputs, unlike strict parsers that would immediately throw errors.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a multi-chunk, incomplete JSON string into a JavaScript object incrementally, simulating a real-time stream. It also shows how to reset the parser for new data.

import { IncompleteJsonParser } from 'incomplete-json-parser';

async function parseStreamingJson() {
  const parser = new IncompleteJsonParser();

  console.log('--- Initializing streaming JSON parsing ---');

  // Simulate receiving data in multiple, incomplete chunks
  parser.write('{"user": {"id": 123, "name": "Alice", "email": "alice@example');
  console.log('Chunk 1 received. Current object:', parser.getObjects());

  parser.write('.com"}, "preferences": {"theme": "dark", "notifications": tru');
  console.log('Chunk 2 received. Current object:', parser.getObjects());

  parser.write('e}, "items": ["apple", "banana", "orange"');
  console.log('Chunk 3 received. Current object:', parser.getObjects());

  parser.write(']}'); // Final closing bracket
  const finalResult = parser.getObjects();
  console.log('Final chunk received. Final object:', finalResult);

  // Demonstrating reset and reuse
  parser.reset();
  console.log('\n--- Parser reset for new data ---');
  parser.write('{"status": "complete", "code": 200}');
  console.log('New data parsed:', parser.getObjects());
}

parseStreamingJson();

view raw JSON →