npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@iamqc/cc-json-parser

v1.1.0

Published

A powerful JSON parser designed specifically for handling LLM responses with multiple extraction strategies and intelligent error handling

Readme

@iamqc/cc-json-parser

npm version npm downloads License: MIT Test Coverage

终极JSON解析器 - 一个方法,搞定所有!

一个专门为处理 LLM 响应设计的强大 JSON 解析工具,基于类型检测的智能策略选择,支持无限格式。

核心优势

  • 一个方法搞定 - 只需 JSONParser.extractJSON(),自动适配所有格式
  • 智能类型检测 - 准确识别7种输入类型,自动选择最优策略
  • 100%成功率 - 通过30项极限测试,完美处理各种边界情况
  • 架构可扩展 - 依赖注入支持自定义检测器、提取器、修复器
  • 全语言支持 - Unicode、Emoji、中文、日文、韩文、阿拉伯文、俄文

安装

bun add @iamqc/cc-json-parser
npm install @iamqc/cc-json-parser
yarn add @iamqc/cc-json-parser
pnpm add @iamqc/cc-json-parser

快速开始

基础使用

import { JSONParser } from '@iamqc/cc-json-parser';

const result = JSONParser.extractJSON(anyText);
// 一个方法搞定所有格式!

支持的格式

// ✅ Markdown代码块
JSONParser.extractJSON('```json\n{"name": "张三"}\n```');

// ✅ 嵌入JSON
JSONParser.extractJSON('结果:{"status": "success"} 时间:2024-01-01');

// ✅ 纯JSON
JSONParser.extractJSON('{"name": "李四", "skills": ["JavaScript"]}');

// ✅ 结构化文本
JSONParser.extractJSON('name: 赵六\nage: 28\ncity: 上海');

// ✅ 自然语言
JSONParser.extractJSON('用户信息:姓名王五,年龄30岁');

架构设计

src/jsonParser.ts
├── 接口层 (Interfaces)
│   ├── JSONRepairStrategy      # JSON修复策略
│   ├── DetectorStrategy        # 类型检测策略
│   └── ExtractionStrategy      # 提取策略
│
├── 工具层 (Utilities)
│   └── JSONParse               # 共享JSON解析+修复
│
├── 检测层 (InputTypeDetector)
│   ├── PureJSONDetector        # 纯JSON检测
│   ├── MarkdownCodeBlockDetector
│   ├── EmbeddedJSONDetector
│   ├── StructuredTextDetector
│   └── NaturalLanguageDetector
│
├── 提取层 (Extraction Strategies)
│   ├── DirectParseStrategy     # 直接解析
│   ├── MarkdownExtraction      # Markdown提取
│   ├── CodeBlockExtraction    # 代码块提取
│   ├── EmbeddedJSONExtraction  # 嵌入JSON提取
│   ├── StructuredTextExtraction# 结构化文本提取
│   └── NaturalLanguageExtractionStrategy
│
└── 核心 (JSONParserImpl)
    └── 协调检测与提取流程

依赖注入

import { JSONParser, InputTypeDetector, DefaultJSONRepair } from '@iamqc/cc-json-parser';

// 默认配置
const parser = new JSONParser();

// 自定义检测器顺序
const customDetector = new InputTypeDetector([
  new EmbeddedJSONDetector(),
  new PureJSONDetector(),
  // ...自定义顺序
]);

// 自定义修复策略
class CustomRepair implements JSONRepairStrategy {
  repair(input: string): string | null {
    // 自定义修复逻辑
    return input.replace(/'/g, '"');
  }
}

const parser = new JSONParser(customDetector, new CustomRepair());

添加新的检测类型

import { JSONParser, DetectorStrategy, InputType } from '@iamqc/cc-json-parser';

class XMLDetector implements DetectorStrategy {
  readonly type: InputType = 'xml';
  matches(text: string): boolean {
    return text.trim().startsWith('<') && text.trim().endsWith('>');
  }
}

const detector = new InputTypeDetector();
detector.addDetector(new XMLDetector());
const parser = new JSONParser(detector);

API 参考

核心方法

// 终极提取方法
extractJSON(text: string): any | null

// 安全解析
safeParse<T>(jsonString: string, options?: SafeParseOptions): ParseResult<T>

// 批量提取数组
extractJSONArray(text: string): any[]

验证工具

validateJSON(data: any, schema: ValidationSchema): ValidationResult
hasJSONStructure(text: string): boolean
isLikelyNonJSON(text: string): boolean

工具方法

cleanJSONString(text: string): string
extractJSONCandidates(text: string): string[]

类型定义

type InputType =
  | 'pure-json'
  | 'markdown-codeblock'
  | 'embedded-json'
  | 'structured-text'
  | 'natural-language'
  | 'mixed-content'
  | 'invalid-json';

interface JSONRepairStrategy {
  repair(input: string): string | null;
}

interface DetectorStrategy {
  readonly type: InputType;
  matches(text: string): boolean;
}

interface ExtractionStrategy {
  extract(text: string): any | null;
}

interface ParseResult<T = any> {
  success: boolean;
  data: T;
  error: string | null;
}

interface ValidationSchema {
  [key: string]: {
    required?: boolean;
    type?: string;
    validate?: (value: any) => boolean;
  };
}

测试覆盖

  • 边界测试: 20/20 ✅
  • 极端测试: 10/10 ✅
  • 总体成功率: 100%

开发

bun install
bun run test/test-edge-cases.ts

许可证

MIT