Skip to content

Instantly share code, notes, and snippets.

@grrowl
Last active July 20, 2023 04:32
Show Gist options
  • Save grrowl/26f817b2a91d862d488256ca8fc3693e to your computer and use it in GitHub Desktop.
Save grrowl/26f817b2a91d862d488256ca8fc3693e to your computer and use it in GitHub Desktop.
JSON.parse with self-healing (for LLM output)
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