Skip to main content

extract_json

Function extract_json 

Source
pub fn extract_json(input: &str) -> Option<&str>
Expand description

Extract the first JSON payload from raw LLM output.

Handles Markdown code fences and surrounding prose. Among the balanced {...} / [...] blocks found, the first one that parses as JSON (directly, or after sanitize_json) is preferred; otherwise the first block is returned as-is (it may be truncated output, which the sanitize stage can close).

Returns None when the input contains no { or [ at all.

ยงExample

use fuzzy_parser::extract_json;

let output = r#"Sure! Here is the result:

```json
{"type": "AddDerive", "target": "User"}
```

Let me know if you need anything else."#;

assert_eq!(
    extract_json(output),
    Some(r#"{"type": "AddDerive", "target": "User"}"#)
);