AnanthaJson.JsonExtractor (anantha_llm_utils v0.1.0)

Copy Markdown View Source

Extracts and parses JSON from LLM responses.

Summary

Functions

Extracts the longest JSON object string from content.

Extracts ALL valid JSON object strings found in content.

Extracts the first JSON array [...] from content.

Extracts the longest (outermost) valid JSON object from content.

Functions

extract(content)

@spec extract(String.t() | nil) :: String.t()

Extracts the longest JSON object string from content.

Tries markdown code blocks first (json or), then falls back to finding the outermost valid JSON object via balanced brace matching. Returns "" if no JSON is found.

extract_all_objects(content)

@spec extract_all_objects(String.t()) :: [String.t()]

Extracts ALL valid JSON object strings found in content.

Returns a list of raw JSON strings, from left to right, or an empty list if none are found.

extract_array(content)

@spec extract_array(String.t() | nil) :: String.t()

Extracts the first JSON array [...] from content.

Tries markdown code blocks first, then falls back to balanced bracket matching. Returns the raw JSON string or "" if none found.

extract_last_json_object(content)

@spec extract_last_json_object(String.t()) :: {:ok, String.t()} | {:error, atom()}

Extracts the longest (outermost) valid JSON object from content.