From 969704f96a662c2a3bb81480ed2b11e190405738 Mon Sep 17 00:00:00 2001 From: Rupayon Haldar <80724680+rupayon123@users.noreply.github.com> Date: Tue, 23 Jun 2026 10:42:08 -0400 Subject: [PATCH 1/2] docs: clarify parsing fenced model output --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index e9ff092..1c4c446 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,17 @@ result = parse("-Inf"); console.log(result); // Outputs: -Infinity ``` +### Parsing model replies with Markdown fences + +`parse` expects the JSON text itself. If a model wraps JSON in Markdown fences or surrounding prose, strip that wrapper before passing the string to `parse`: + +````js +const reply = "```json\n{\"status\": \"ok\"}\n```"; +const json = reply.replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/, ""); + +parse(json); // Outputs: { status: "ok" } +```` + ### Handling malformed JSON If the JSON string is malformed, the `parse` function will throw an error: From 4ea5649ec476175491d22542119695f15f1a1a12 Mon Sep 17 00:00:00 2001 From: Rupayon Haldar <80724680+rupayon123@users.noreply.github.com> Date: Tue, 23 Jun 2026 10:56:47 -0400 Subject: [PATCH 2/2] docs: handle prose around fenced JSON --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c4c446..f71100d 100644 --- a/README.md +++ b/README.md @@ -77,8 +77,9 @@ console.log(result); // Outputs: -Infinity `parse` expects the JSON text itself. If a model wraps JSON in Markdown fences or surrounding prose, strip that wrapper before passing the string to `parse`: ````js -const reply = "```json\n{\"status\": \"ok\"}\n```"; -const json = reply.replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/, ""); +const reply = "Here is the JSON:\n```json\n{\"status\": \"ok\"}\n```"; +const match = reply.match(/```(?:json)?\s*([\s\S]*?)\s*```/i); +const json = match ? match[1] : reply; parse(json); // Outputs: { status: "ok" } ````