ai-output-extract-json Edge Function — Ai
AIExtracts the first valid JSON object or array embedded in LLM free-text output, handling markdown code fences.
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.
npx aerostack add navin/ai-output-extract-json Use with AI Assistants
MCPConnect 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.
{
"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({
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
- Markdown code fences --
\``json ... ```or``` ... ```` are tried first - Raw JSON blocks -- scans for
{ ... }or[ ... ]with matching brace/bracket depth - 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", ortrueare rejected. - Always returns
{ success: true, data: ... }-- never throws. Parse failures simply result infound: false.
Metadata
Tags
Publisher
@navin verified
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 →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-context-window-fit
by @navin
Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
ai-cost-estimate
by @navin
Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-language-detect
by @navin
Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
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.