PtcRunner.SubAgent.JsonParser (PtcRunner v0.7.0)

Copy Markdown View Source

Extracts JSON from LLM responses, handling common formatting quirks.

LLMs often wrap JSON in markdown code blocks, add explanatory text, or include trailing commentary. This module extracts the JSON content following a priority order:

  1. JSON in json code block 2. JSON in code block (no language)
  2. Raw JSON object (starts with {)
  3. Raw JSON array (starts with [)

Examples

iex> PtcRunner.SubAgent.JsonParser.parse(~s|{"name": "Alice"}|)
{:ok, %{"name" => "Alice"}}

iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{\"a\": 1}\n```")
{:ok, %{"a" => 1}}

iex> PtcRunner.SubAgent.JsonParser.parse("Here's the result: {\"x\": 5}")
{:ok, %{"x" => 5}}

iex> PtcRunner.SubAgent.JsonParser.parse("No JSON here")
{:error, :no_json_found}

Summary

Functions

Parse JSON from an LLM response string.

Functions

parse(response)

@spec parse(String.t()) :: {:ok, term()} | {:error, :no_json_found | :invalid_json}

Parse JSON from an LLM response string.

Extracts JSON from code blocks or raw content, handling common LLM formatting quirks like trailing text or explanation prefixes.

Returns {:ok, term()} with the parsed JSON data, or an error tuple.

Error Types

  • {:error, :no_json_found} - No JSON structure detected in the response
  • {:error, :invalid_json} - JSON was found but failed to parse

Examples

iex> PtcRunner.SubAgent.JsonParser.parse(~s|{"count": 42}|)
{:ok, %{"count" => 42}}

iex> PtcRunner.SubAgent.JsonParser.parse("[1, 2, 3]")
{:ok, [1, 2, 3]}

iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{\"valid\": true}\n```")
{:ok, %{"valid" => true}}

iex> PtcRunner.SubAgent.JsonParser.parse("plain text")
{:error, :no_json_found}

iex> PtcRunner.SubAgent.JsonParser.parse("```json\n{invalid}\n```")
{:error, :invalid_json}