Last active
July 20, 2023 04:32
-
-
Save grrowl/26f817b2a91d862d488256ca8fc3693e to your computer and use it in GitHub Desktop.
JSON.parse with self-healing (for LLM output)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { jsonrepair } from "jsonrepair"; | |
export function tryParseJson<T>(json: string): T | undefined { | |
try { | |
return JSON.parse(jsonrepair(json)); | |
} catch (error) { | |
// throw new Error(`Failed to parse JSON ${error}`); | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment