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

safe-json-extract

v1.1.0

Published

A zero-dependency JavaScript library for safely extracting, repairing, and parsing JSON from LLM-generated outputs.

Readme

safe-json-extract

A production-grade, zero-dependency JavaScript library for safely extracting, repairing, and parsing JSON from LLM-generated outputs.


Built for AI systems that return unreliable JSON

Modern LLMs often produce structured data that is not safely parseable.

This library is designed to handle outputs from:

  • OpenAI
  • Anthropic Claude
  • Google Gemini
  • xAI Grok
  • Alibaba Qwen
  • DeepSeek
  • Ollama

Why this library exists

When working with LLMs in production, JSON responses are often:

  • mixed with natural language
  • wrapped in markdown
  • truncated mid-response
  • using invalid syntax (single quotes, trailing commas)
  • partially structured but not machine-safe

This creates a recurring problem:

The output looks like JSON, but cannot be safely parsed.

safe-json-extract solves this by providing a deterministic extraction and repair layer.


Key principles

  • Zero dependencies
  • Deterministic output
  • Pure functions
  • No side effects
  • Works in Node.js, Bun, Deno, and browsers
  • No telemetry or data collection

Installation

npm install safe-json-extract

Quick start

const { parseLLMResponse } = require('safe-json-extract');

const input = `
Here is the response:

{"name":"Eliezer", "age":42}
`;

const result = parseLLMResponse(input);

console.log(result);

Output example

{
  ok: true,
  repaired: false,
  data: {
    name: "Eliezer",
    age: 42
  }
}

Core API

Extraction functions

  • extractJson(text)
  • extractArray(text)
  • extractCodeBlock(text)
  • extractAllJson(text)

Validation functions

  • isJson(text)
  • safeParse(text)

Transformation functions

  • repairJson(text)
  • normalizeJson(object)

High-level function

  • parseLLMResponse(text)

What makes this library different

Most JSON parsers assume valid input.

This library assumes the opposite:

Real-world AI output is messy, inconsistent, and partially invalid.

Instead of failing, it:

  • extracts valid structures
  • repairs common formatting issues
  • safely returns structured results
  • avoids throwing runtime exceptions

Use cases

  • AI agent development
  • LLM API integrations
  • Backend data pipelines
  • Log parsing systems
  • Structured response extraction
  • Automation systems using GPT-like models

Design philosophy

This library is not a replacement for JSON.parse.

It is a reliability layer between LLM outputs and your application logic.

Its goal is to make unpredictable outputs usable in production systems.


Error handling model

Instead of throwing errors, the library always returns structured results:

{
  ok: true | false,
  repaired: true | false,
  data: object | null,
  error?: string
}

This ensures predictable control flow.


Compatibility

  • Node.js (LTS)
  • Bun
  • Deno
  • Browser environments

Performance

  • No dependencies
  • Lightweight execution
  • Designed for high-frequency parsing
  • Safe for backend pipelines

Author

Eliezer Tavares de Oliveira
Computer Engineering Student — UNIVESP

GitHub: https://github.com/eliezer-tavares
Email: [email protected]


License

MIT License

This project is free to use, modify, and distribute.


Contributing

Contributions are welcome.

You can help by:

  • reporting edge cases from LLM outputs
  • improving parsing robustness
  • suggesting new extraction utilities
  • optimizing performance

Roadmap

v1.1

  • improved markdown extraction
  • normalization improvements
  • better partial JSON recovery

v1.2

  • streaming-safe parsing
  • chunked LLM response handling

v2.0

  • plugin system for LLM providers
  • structured adapters for OpenAI, Grok, Qwen, Claude

Final note

If you have ever received an LLM response that is "almost JSON but not quite valid", this library was built for that exact problem.