Aerostack
memory

ai-output-extract-json Edge Function — Ai

AI

Extracts the first valid JSON object or array embedded in LLM free-text output, handling markdown code fences.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function ai-output-extract-json Extracts the first valid JSON object or array embedded in LLM free-text output, handling markdown code fences.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/ai-output-extract-json
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "ai-output-extract-json",
  args: {
    "text": "example_text",
    "strict": false
  }
})

ai-output-extract-json

Extracts the first valid JSON object or array embedded in LLM free-text output. Handles markdown code fences (\``json ... ```) and falls back to scanning for raw { }or[ ]` blocks.

Install

aerostack function add ai-output-extract-json

Usage

import { extractJson } from './functions/ai-output-extract-json';

const llmOutput = `Here is the data:
\`\`\`json
{"name": "Alice", "age": 30}
\`\`\`
Hope that helps!`;

const result = extractJson(llmOutput);
// { success: true, data: { json: { name: "Alice", age: 30 }, raw: '{"name": "Alice", "age": 30}', found: true } }

// Strict mode: only accept objects and arrays
const strictResult = extractJson('```json\n42\n```', true);
// { success: true, data: { json: null, raw: '', found: false } }

API

POST /api/ai-output-extract-json

Request

{
  "text": "The result is: {\"status\": \"ok\", \"count\": 42}",
  "strict": false
}

Response

{
  "success": true,
  "data": {
    "json": { "status": "ok", "count": 42 },
    "raw": "{\"status\": \"ok\", \"count\": 42}",
    "found": true
  }
}
Field Type Required Default
text string Yes --
strict boolean No false

Extraction Priority

  1. Markdown code fences -- \``json ... ```or``` ... ```` are tried first
  2. Raw JSON blocks -- scans for { ... } or [ ... ] with matching brace/bracket depth
  3. Not found -- returns { json: null, raw: "", found: false }

Notes

  • Handles nested objects, escaped quotes, and braces inside string values.
  • In strict mode, only objects and arrays are accepted; primitives like 42, "hello", or true are rejected.
  • Always returns { success: true, data: ... } -- never throws. Parse failures simply result in found: false.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

ai
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More AI Functions

Browse AI Functions →

Frequently asked questions

What does the ai-output-extract-json function do? +

ai-output-extract-json is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the ai-output-extract-json function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-output-extract-json ``` It will be live on Cloudflare Workers in seconds.

What runtime does ai-output-extract-json use? +

ai-output-extract-json runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the ai-output-extract-json function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.