MCPcopy Index your code
hub / github.com/coder/coder / extractJSON

Function extractJSON

coderd/taskname/taskname.go:105–117  ·  view source on GitHub ↗

extractJSON strips optional markdown code fences (```json or ```) that LLMs sometimes wrap around JSON output, returning only the inner JSON string. If the response starts with JSON, it returns the first JSON value so trailing commentary or dangling fences do not break parsing.

(s string)

Source from the content-addressed store, hash-verified

103// string. If the response starts with JSON, it returns the first JSON value so
104// trailing commentary or dangling fences do not break parsing.
105func extractJSON(s string) string {
106 s = strings.TrimSpace(s)
107 if matches := markdownCodeFenceRE.FindStringSubmatch(s); matches != nil {
108 s = strings.TrimSpace(matches[1])
109 }
110
111 var raw json.RawMessage
112 if err := json.NewDecoder(strings.NewReader(s)).Decode(&raw); err == nil {
113 return string(raw)
114 }
115
116 return s
117}
118
119type TaskName struct {
120 Name string `json:"task_name"`

Callers 2

generateFromAnthropicFunction · 0.85
TestExtractJSONFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestExtractJSONFunction · 0.68