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

ai-json-parse

v0.1.0

Published

Extract and parse the first JSON block from LLM output — OpenAI, Claude, Gemini markdown fences or inline JSON. Zero deps.

Readme

ai-json-parse

npm version license Node bundle size

Parse JSON from LLM text — even when the model wraps it in markdown, adds explanations, or embeds { ... } inside a sentence.

Zero dependencies · TypeScript types · ESM + CommonJS · ~3 KB minified


The problem

Models rarely return clean JSON. You often get prose + a ```json fenced block + a closing sentence.

JSON.parse(entireMessage) fails. Regex breaks on nested braces and strings. ai-json-parse finds the first valid JSON block and parses it.


Install

npm install ai-json-parse
yarn add ai-json-parse
pnpm add ai-json-parse

Quick start

import { parseFirstJson } from "ai-json-parse";

const reply = await callOpenAI(); // string from chat completion

const data = parseFirstJson<{ status: string; items: number[] }>(reply);
console.log(data.status); // "ok"

Examples

Markdown json fence (most common)

Input (LLM output):

Here are the users:

```json
{"users": [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Lin"}]}
```

Code:

import { parseFirstJson } from "ai-json-parse";

const users = parseFirstJson(llmText);
// { users: [{ id: 1, name: "Ada" }, { id: 2, name: "Lin" }] }

Generic code fence

Input: a fenced block containing ["pending", "done", "failed"]

Output: ["pending", "done", "failed"]


Inline JSON in prose

Input:

Use this config for local dev: {"port": 3000, "debug": true} and restart the server.

Output: { port: 3000, debug: true }


OpenAI / structured-style response

import OpenAI from "openai";
import { parseFirstJson } from "ai-json-parse";

const openai = new OpenAI();

const completion = await openai.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    {
      role: "user",
      content: "Return JSON with fields city and temp_c only. No other text.",
    },
  ],
});

const text = completion.choices[0]?.message?.content ?? "";
const weather = parseFirstJson<{ city: string; temp_c: number }>(text);

Safe parsing (no throw)

import { tryParseFirstJson } from "ai-json-parse";

const data = tryParseFirstJson(maybeNotJson);
if (data === null) {
  // fallback: retry prompt, log, etc.
}

Raw JSON string only

import { extractFirstJsonString } from "ai-json-parse";

const raw = extractFirstJsonString(llmText);
// '{"users":[...]}'  — validate or pass to another parser yourself

API

| Export | Description | |--------|-------------| | parseFirstJson<T>(text, options?) | Returns first parsed JSON value. Throws JsonExtractError if none found (default). | | tryParseFirstJson<T>(text) | Same as parseFirstJson(text, { strict: false })T \| null. | | extractFirstJsonString(text) | Returns raw JSON string; throws if not found. | | JsonExtractError | Error when no block exists or JSON.parse fails. |

Options

parseFirstJson(text, { strict: false }); // returns null instead of throwing

How “first” is chosen

Candidates are sorted by position in the string (earliest wins). The first block that passes JSON.parse is returned.

  1. ```json ... ``` fenced blocks
  2. Other ``` ... ``` blocks that start with { or [
  3. Inline balanced {...} or [...] (string-aware, handles \" and { inside strings)

Comparison

| Approach | LLM markdown + prose | |----------|----------------------| | JSON.parse(fullText) | Fails | | Naive regex | Breaks on nested {} and strings | | ai-json-parse | Handles fences + inline JSON | | JSON5 / repair libs | Different goal (fix invalid JSON); use alongside if needed |


Limits

  • Strict JSON only — no trailing commas, comments, or single-quoted keys.
  • Returns the first valid block; later blocks are ignored.
  • Not for security — this is developer ergonomics, not encryption or hiding data.

Requirements

  • Node.js 18+
  • Works in bundlers (Vite, webpack, Next.js) and serverless

Development

Clone and test locally:

git clone https://github.com/monil80/ai-json-parse.git
cd ai-json-parse
npm install
npm test
npm run build
npm run example

Related keywords

llm openai chatgpt claude gemini structured output json extract markdown code block agent function calling


Links