Skip to main content

extract_json

Function extract_json 

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

Pull a JSON object out of a reply that may be fenced or prefaced.

Models wrap JSON in ```json fences, or preface it with a sentence, often enough that requiring a bare object would fail on working output.

Two implementations of this used to live in two crates. The naive one took everything between the first { and the last }, so a reply holding an object followed by any later brace — a second example, a closing fence with a brace in it, prose about {} — parsed as nothing. This is the scanner: the first balanced object, respecting strings and escapes.