Files
deer-flow/web/src/core/utils/json.ts
T
2025-06-12 19:00:08 +08:00

21 lines
475 B
TypeScript

import { parse } from "best-effort-json-parser";
export function parseJSON<T>(json: string | null | undefined, fallback: T) {
if (!json) {
return fallback;
}
try {
const raw = json
.trim()
.replace(/^```js\s*/, "")
.replace(/^```json\s*/, "")
.replace(/^```ts\s*/, "")
.replace(/^```plaintext\s*/, "")
.replace(/^```\s*/, "")
.replace(/\s*```$/, "");
return parse(raw) as T;
} catch {
return fallback;
}
}