@ axync/extract-json (英语)
概览
@axync/extract-json 是一种强大的工具,旨在从原始文本中提取有效的JSON对象和阵列。 无论是与大文本数据合作,处理日志,还是解析大语言模型(LLMs)的响应,这个软件包都有助于从无结构的字符串中高效地取出结构化的JSON数据.
关键特征包括:
- 同步和流式支持:在不阻碍事件循环的情况下,逐步处理大型字符串.
- 弹性 JSON 采掘业: 从混合或吵闹的文本中提取多个 JSON 对象或阵列.
- 实时数据处理:用于日志解析或API响应处理等实时应用程序的理想.
- 恢复能力:在继续解析有效数据的同时优雅地跳过无效的 JSON .
说明: 这个软件包侧重于取出JSON对象(
{})和数组([]仅此而已。 其他的JSON类型如字符串,数字,或布尔等都不被支持.
与 @axync/extract-json,可以无缝地将 JSON 提取集成到您的工作流程中,使其成为开发者使用无结构文本或LLM输出工作的基本工具.
安装
您可以使用 npm 安装软件包 :
npm install @axync/extract-json
使用量
这套方案提供的主要职能是: extractJson (同步)和 extractJsonSync (同步),它允许您从原始字符串中提取 JSON 对象和阵列 .
示例( Async)
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]]
示例(同步流)
这个 extractStream 函数允许您渐进处理 JSON 对象,这对大字符串或实时数据处理有用.
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" }
用限制提取
也可以指定一个限制来控制 JSON 对象或被提取的数组的数量:
自动同步
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 }]
API 密码
extractJson(rawString: string, limit?: number): Promise<T[]>
- 原始结构:包含潜在 JSON 对象或阵列的字符串.
- 限制: (可选) 要提取的 JSON 对象/阵列的最大数量 。 默认为
Infinity。 。 。 .
回返页:1 Promise 以解析 JSON 对象和数组.
extractJsonSync(rawString: string, limit?: number): T[]
- 原始结构:包含潜在 JSON 对象或阵列的字符串.
- 限制: (可选) 要提取的 JSON 对象/阵列的最大数量 。 默认为
Infinity。 。 。 .
回返:取出JSON对象和阵列的阵列.
extractStream(rawString: string): AsyncGenerator<T>
- 原始结构:包含潜在 JSON 对象或阵列的字符串.
回返页:1 AsyncGenerator 使 JSON 对象和阵列在解析时产生.
如何运作
-
直接解析编号:
JsonExtractor类首尝试直接将整个字符串解析为JSON. -
查找启动索引: 如果直接解析失败, 它扫描 JSON 对象的潜在启动索引的字符串(
{, (中文(简体) ),[)和数组. -
分析子字符串:该类然后试图从每个已识别索引开始分析子字符串,搜索有效的JSON对象和数组.
-
流线编号:
extractStream方法将字符串递增处理,并在解析时输出 JSON 对象.
业绩
进行了业绩测试,以评价以下三项主要职能的效率: @axync/extract-json。下面是处理10万个JSON对象和一个阵列的结果:
- 摘录 Json: 已处理
86.25ms。 。 。 . - 提取 Json 同步: 已处理
78.93ms。 。 。 . - 提取出 Stream: 已处理
108.98ms。 。 。 .
测试环境
- CPU 软件: 第13回 Gen Intel(R) Core(TM) i9-13900K, 24个核心, 3.0 GHz
意 见
extractJsonSync是同步操作中最快的, 但会阻断事件循环 .extractJson提供同步处理,使其适合非阻塞操作.extractStream是逐渐流出大数据的理想方法.
限制
- 此软件包只提取 JSON 对象(
{})和数组([]) (中文(简体) ). 它不提取JSON的其他数据类型,如字符串,数字,或布尔.
许可证
这个项目根据麻省理工学院的许可证获得许可.