Socket
Book a DemoInstallSign in
Socket

@selaliadobor/incomplete-json-parser

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@selaliadobor/incomplete-json-parser

A JSON parser that can parse incomplete JSON strings.

1.1.8
latest
npmnpm
Version published
Weekly downloads
14
250%
Maintainers
1
Weekly downloads
 
Created
Source

incomplete-json-parser

A JSON parser that can parse incomplete JSON strings.

Demo

  • Incomplete JSON vs Parse-able JSON

demo-small

  • Application example
application-example

What is incomplete-json-parser?

incomplete-json-parser is a TypeScript module that provides a streaming JSON parser. It can handle incomplete or chunked JSON data, making it useful for parsing JSON data that arrives in multiple parts or when dealing with large JSON files.

The parser is designed to be flexible and can handle various scenarios, such as:

  • Incomplete JSON objects or arrays
  • JSON data split across multiple chunks
  • Incomplete string values

Installation

To install incomplete-json-parser, use the following command:

npm install incomplete-json-parser
yarn add incomplete-json-parser

Usage

Here's an example of how to use incomplete-json-parser:

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

const parser = new IncompleteJsonParser();

// Write incomplete JSON data to the parser
parser.write('{"name": "John", "age": 30, "city": "New');
parser.write(' York", "hobbies": ["reading", "gaming"');

// Get the parsed JavaScript object
const result = parser.getObjects();
console.log(result);
// Output: { name: 'John', age: 30, city: 'New York', hobbies: ['reading', 'gaming'] }

In this example, we create an instance of the IncompleteJsonParser and write incomplete JSON data to it using the write method. We can write data in multiple chunks, simulating a streaming scenario.

Once we have written all the necessary data, we call the getObjects method to parse the accumulated JSON data and retrieve the resulting JavaScript object.

API

new IncompleteJsonParser()

Creates a new instance of the IncompleteJsonParser.

static parse(chunk: string): any

A static method that allows parsing JSON data in a single step. It creates a new instance of IncompleteJsonParser, writes the provided chunk to it, and returns the parsed JavaScript object.

Using the parse Static Method

const json = '{"name": "Alice", "age": 25, "city": "London"';
const result = IncompleteJsonParser.parse(json);
console.log(result);
// Output: { name: 'Alice', age: 25, city: 'London' }

reset(): void

Resets the parser's internal state, clearing the buffer, accumulator, pointer, and path. This method is useful when you want to reuse the same parser instance for parsing multiple JSON objects.

write(chunk: string): void

Writes a chunk of JSON data to the parser's internal buffer.

getObjects(): any

Parses the accumulated JSON data and returns the parsed JavaScript object.

Examples

Here are a few more examples demonstrating the capabilities of incomplete-json-parser:

Handling Incomplete JSON Objects

const parser = new IncompleteJsonParser();

parser.write('{"name": "Alice", "age": 25, "city": "London"');
const result = parser.getObjects();
console.log(result);
// Output: { name: 'Alice', age: 25, city: 'London' }

Handling Incomplete JSON Arrays

const parser = new IncompleteJsonParser();

parser.write('["apple", "banana", "orange"');
const result = parser.getObjects();
console.log(result);
// Output: ['apple', 'banana', 'orange']

Handling Incomplete String Values

const parser = new IncompleteJsonParser();

parser.write('{"message": "Hello, world!');
const result = parser.getObjects();
console.log(result);
// Output: { message: 'Hello, world!' }

Handling null Values with Different Lengths

const parser = new IncompleteJsonParser();

parser.write('{"value": n');
const result1 = parser.getObjects();
console.log(result1);
// Output: { value: null }

parser.write('{"value": nu');
const result2 = parser.getObjects();
console.log(result2);
// Output: { value: null }

Author

👤 Dante Chun

  • Website: https://dante.company
  • Github: @1000ship

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 Dante Chun.
This project is MIT licensed.

Keywords

json

FAQs

Package last updated on 29 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.