Getting Started
Install
bash
npm install json-schema-sketchQuick Example
typescript
import { inferSchema, renderSchema } from "json-schema-sketch";
const apiResponse = {
data: [
{ id: 1, name: "Alice", email: "alice@example.com" },
{ id: 2, name: "Bob", email: "bob@example.com" },
],
meta: { page: 1, total: 50 },
};
const schema = inferSchema(apiResponse);
const output = renderSchema(schema);
console.log(output);Output:
data: array(2) of {id: number, name: string, email: string}
meta: {page: number, total: number}The raw JSON is ~200 characters. The schema is ~80 characters — 60% savings.
Why?
When using LLMs with tool-use (function calling), you often need to describe the shape of a JSON response without sending the entire payload. This library produces minimal type descriptions that tell the LLM what fields exist and what types they are, without wasting tokens on actual values.
Two Modes
typescript
// Default: samples 3 elements (fast, good for uniform API responses)
inferSchema(data);
inferSchema(data, { mode: "sampled" });
// Exhaustive: checks every element (catches mixed types)
inferSchema(data, { mode: "exhaustive" });Path Resolution
typescript
import { resolvePath } from "json-schema-sketch";
const result = resolvePath(apiResponse, "data[0].name");
// { ok: true, value: "Alice", isWildcard: false }
const all = resolvePath(apiResponse, "data[*].name");
// { ok: true, value: ["Alice", "Bob"], isWildcard: true, totalItems: 2 }