
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
@axync/extract-json
Advanced tools
@axync/extract-json is a powerful utility designed to extract valid JSON objects and arrays from raw text. Whether you're working with large text data, processing logs, or parsing responses from Large Language Models (LLMs), this package helps you efficiently extract structured JSON data from unstructured strings.
Key features include:
Note: This package focuses on extracting JSON objects (
{}) and arrays ([]) only. Other JSON types like strings, numbers, or booleans are not supported.
With @axync/extract-json, you can seamlessly integrate JSON extraction into your workflows, making it an essential tool for developers working with unstructured text or LLM outputs.
You can install the package using npm:
npm install @axync/extract-json
The primary functions provided by this package are extractJson (asynchronous) and extractJsonSync (synchronous), which allow you to extract JSON objects and arrays from a raw string.
import { extractJson } from '@axync/extract-json';
const rawString = `
Here is some text before a JSON object: {"key": "value"}
and another one: {"anotherKey": 123} and here is an array: [1, 2, 3]
`;
const jsonObjects = await extractJson(rawString);
console.log(jsonObjects);
// Output: [{ "key": "value" }, { "anotherKey": 123 }, [1, 2, 3]]
import { extractJsonSync } from '@axync/extract-json';
const rawString = `
Here is some text before a JSON object: {"key": "value"}
and another one: {"anotherKey": 123} and here is an array: [1, 2, 3]
`;
const jsonObjects = extractJsonSync(rawString);
console.log(jsonObjects);
// Output: [{ "key": "value" }, { "anotherKey": 123 }, [1, 2, 3]]
The extractStream function allows you to process JSON objects incrementally, which is useful for large strings or real-time data processing.
import { extractStream } from '@axync/extract-json';
const rawString = `
{"key1": "value1"} {"key2": "value2"} {"key3": "value3"}
`;
for await (const json of extractStream(rawString)) {
console.log(json);
}
// Output:
// { key1: "value1" }
// { key2: "value2" }
// { key3: "value3" }
You can also specify a limit to control the number of JSON objects or arrays extracted:
const jsonObjects = await extractJson(rawString, 2);
console.log(jsonObjects);
// Output: [{ "key": "value" }, { "anotherKey": 123 }]
const jsonObjects = extractJsonSync(rawString, 2);
console.log(jsonObjects);
// Output: [{ "key": "value" }, { "anotherKey": 123 }]
extractJson(rawString: string, limit?: number): Promise<T[]>Infinity.Returns: A Promise that resolves to an array of extracted JSON objects and arrays.
extractJsonSync(rawString: string, limit?: number): T[]Infinity.Returns: An array of extracted JSON objects and arrays.
extractStream(rawString: string): AsyncGenerator<T>Returns: An AsyncGenerator that yields JSON objects and arrays as they are parsed.
Direct Parsing: The JsonExtractor class first attempts to directly parse the entire string as JSON.
Finding Start Indexes: If direct parsing fails, it scans the string for potential start indexes of JSON objects ({, [) and arrays.
Parsing Substrings: The class then attempts to parse substrings starting from each identified index, searching for valid JSON objects and arrays.
Streaming: The extractStream method processes the string incrementally and yields JSON objects as they are parsed.
Performance tests were conducted to evaluate the efficiency of the three primary functions provided by @axync/extract-json. Below are the results for processing 100,000 JSON objects and an array:
86.25ms.78.93ms.108.98ms.extractJsonSync is the fastest for synchronous operations but blocks the event loop.extractJson provides asynchronous processing, making it suitable for non-blocking operations.extractStream is ideal for streaming large data incrementally.{}) and arrays ([]). It does not extract other JSON data types like strings, numbers, or booleans.This project is licensed under the MIT License.
FAQs
An utility to extract JSON object and array from a string.
The npm package @axync/extract-json receives a total of 437 weekly downloads. As such, @axync/extract-json popularity was classified as not popular.
We found that @axync/extract-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.