Function: extractJson()
extractJson(
rawText):string|null
Defined in: packages/agentos/src/core/validation/extractJson.ts:35
Extract JSON from raw LLM output text.
Tries multiple extraction strategies in priority order:
- Raw JSON (entire string is valid JSON)
- Markdown fenced blocks (
json ...or...) - Strip
<thinking>blocks, then retry - First
{...}or[...]via greedy brace/bracket matching - JSONL (multiple JSON objects on separate lines → array)
Parameters
rawText
string
Raw LLM output that may contain JSON
Returns
string | null
Extracted JSON string, or null if no valid JSON found
Example
extractJson('```json\n{"key": "value"}\n```') // '{"key": "value"}'
extractJson('<thinking>hmm</thinking>\n{"a":1}') // '{"a":1}'
extractJson('no json here') // null