MCPcopy Create free account
hub / github.com/Noumena-Network/code / extractJsonPayload

Function extractJsonPayload

src/tools/WebFetchTool/sourceProvenance.ts:100–127  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

98}
99
100function extractJsonPayload(text: string):
101 | {
102 jsonText: string
103 wrap: (jsonText: string) => string
104 pretty: boolean
105 }
106 | null {
107 const trimmed = text.trim()
108 const fenced = trimmed.match(/^```(?:json)?\s*\n([\s\S]*?)\n```$/)
109 if (fenced?.[1]) {
110 return {
111 jsonText: fenced[1],
112 wrap: jsonText => `\`\`\`json\n${jsonText}\n\`\`\``,
113 pretty: true,
114 }
115 }
116 if (
117 (trimmed.startsWith('{') && trimmed.endsWith('}')) ||
118 (trimmed.startsWith('[') && trimmed.endsWith(']'))
119 ) {
120 return {
121 jsonText: trimmed,
122 wrap: jsonText => jsonText,
123 pretty: trimmed.includes('\n'),
124 }
125 }
126 return null
127}
128
129function sourceMap(
130 sources: readonly WebFetchSourceMetadata[],

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected