INNER CODE UNIT · TypeScript
extractJson
austin-starks/Promptimizer · src/utils/index.ts:33
export function extractJson(text: string): Record<string, any> | undefined {
if (!text) {
return undefined;
}
// Use regex to find the JSON content within the text
const jsonMatch = text.match(/{[\s\S]*}/);
if (!jsonMatch) {
return undefined;
}
// Extract the JSON string from the matched content
let jsonString = jsonMatch[0];
// Remove known non-JSON markers and sanitize the string
jsonString = jsonString
.replace(/```SQL/g, "")
.replace(/```sql/g, "")