Incomplete JSON Parser
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
-
TypeError: IncompleteJsonParser is not a constructor
cause Attempting to use `require` for `IncompleteJsonParser` in a CommonJS context without correctly accessing the named export, or incorrect named import syntax in an ESM environment.fixFor ESM, ensure `import { IncompleteJsonParser } from 'incomplete-json-parser';` is used. For CommonJS, use `const { IncompleteJsonParser } = require('incomplete-json-parser');`. -
Parsed object is empty or does not reflect recent `write()` calls.
cause Forgetting to call `getObjects()` after `write()` operations to retrieve the current parsed state, or not properly resetting the parser between processing different JSON inputs.fixEnsure `getObjects()` is called to retrieve the current state of the parsed object. If parsing multiple independent JSON strings, call `parser.reset()` before feeding new data to the same parser instance. -
Resulting object contains unexpected partial or malformed data that was expected to be completed.
cause The input stream, even with its fault tolerance, contained genuinely malformed JSON (e.g., deeply nested unclosed structures, invalid characters within values) that could not be reconciled into a valid partial object, or the stream ended prematurely without enough context for the parser to infer a complete structure.fixReview the source JSON stream for fundamental syntax errors beyond simple incompleteness. While the parser is robust, it cannot correct arbitrarily invalid JSON. Implement robust error handling or logging around the input source.
Warnings
- gotcha Incomplete JSON Parser is designed for fault tolerance and will attempt to yield a valid partial object from incomplete input. This means it may *not* throw an error where a strict `JSON.parse` would, potentially masking truly malformed JSON if not explicitly handled.
- gotcha When reusing an `IncompleteJsonParser` instance for multiple distinct JSON strings, it's crucial to call `parser.reset()` before processing the next string. Failing to do so will cause subsequent data to be appended to the previously buffered content, leading to incorrect parsing.
- gotcha This library processes string data and does not perform deep validation on the *meaning* or *schema* of the JSON. It primarily focuses on structural integrity to the extent possible with incomplete data. Trusting arbitrary external input directly without further sanitization could pose risks if the downstream application expects a specific data shape or content.
Install
-
npm install incomplete-json-parser -
yarn add incomplete-json-parser -
pnpm add incomplete-json-parser
Imports
- IncompleteJsonParser
const IncompleteJsonParser = require('incomplete-json-parser');import { IncompleteJsonParser } from 'incomplete-json-parser'; - IncompleteJsonParser.parse
import { parse } from 'incomplete-json-parser';const result = IncompleteJsonParser.parse('{"data": "value"'); - IncompleteJsonParser (type)
import type { IncompleteJsonParser } from 'incomplete-json-parser';
Quickstart
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();