@iconicompany/sanitizejson
v1.0.1
Published
Sanitize and repair JSON strings from AI responses
Downloads
269
Readme
@iconicompany/sanitizejson
A utility to sanitize and repair malformed JSON strings, especially those generated by AI models.
Installation
bun add @iconicompany/sanitizejsonUsage
import { cleanJsonString } from "@iconicompany/sanitizejson";
const rawAiResponse = "Here is the JSON:\n```json\n{ key: \"value\", }\n```";
const cleanJson = cleanJsonString(rawAiResponse);
const data = JSON.parse(cleanJson);
console.log(data); // { key: "value" }Features
- Markdown Extraction: Automatically extracts JSON from
json ...or...blocks. - Structural Repair: Uses
@qraftr/json-repairto fix unquoted keys, trailing commas, and missing brackets. - Semantic Sanitization:
- Merges literal newlines inside strings.
- Fixes invalid backslashes (e.g., in Windows paths).
- Replaces curly quotes with standard ones.
- Removes control characters and Unicode replacement characters.
License
MIT
