utils/json

v2.0.1

Robust JSON parsing and validation with schema support.

2.1Kweekly downloads
Published 1 year ago
MIT License
spm install utils/json

Readme

utils/json

Robust JSON parsing and validation with schema support, designed specifically for ensuring LLM outputs conform to expected structures.

Features

  • Strict Validation: Validate parsed JSON against JSON Schema.
  • Auto-Fixing: Heuristics to fix common LLM JSON formatting errors (e.g., missing quotes, trailing commas).

Installation

spm install utils/json

Usage

import { parseAndValidate } from "utils/json";

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" }
  },
  required: ["name", "age"]
};

// LLM output with a trailing comma
const rawOutput = '{ "name": "Alice", "age": 30, }';

const data = parseAndValidate(rawOutput, schema);
// Successfully parses and validates!

About

Author
utils


Version History

  • v2.0.11 year ago