MCPcopy Create free account
hub / github.com/MigoXLab/coderio / extractJSON

Function extractJSON

src/utils/parser.ts:20–38  ·  view source on GitHub ↗
(response: string)

Source from the content-addressed store, hash-verified

18 * // Output: "{\"key\": \"value\"}"
19 */
20export function extractJSON(response: string): string {
21 // Try to match ```json ... ``` format first
22 const jsonBlockMatch = response.match(/```json\s*\n([\s\S]*?)\n```/);
23 if (jsonBlockMatch && jsonBlockMatch[1]) {
24 return jsonBlockMatch[1].trim();
25 }
26
27 // Try to match generic ``` ... ``` format (with newlines around content)
28 const codeBlockMatch = response.match(/```\s*\n([\s\S]*?)\n```/);
29 if (codeBlockMatch && codeBlockMatch[1]) {
30 return codeBlockMatch[1].trim();
31 }
32
33 // Fallback: no proper code block; use full content but strip loose markdown fences.
34 // Some models return raw JSON with trailing "```" (e.g. ...]```) or leading ```\n.
35 let cleaned = response.trim();
36 cleaned = cleaned.replace(/^\s*```(?:json)?\s*\n?/g, '').replace(/\s*```+\s*$/g, '');
37 return cleaned;
38}
39
40/**
41 * Extract code content from markdown code blocks

Callers 5

generateStructureFunction · 0.90
populateComponentPropsFunction · 0.90
parser.test.tsFile · 0.90
mainFunction · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected