llm-json-compact
v1.0.0
Published
Compact JSON into a table-like structure that removes repeated keys and is easier for LLMs to consume with fewer tokens.
Maintainers
Readme
llm-json-compact
Compact JSON into a table-like structure that removes repeated keys and is easier for LLMs to consume with fewer tokens.
Instead of repeating field names on every object, llm-json-compact lifts shared keys into a header row and keeps only the values in each row.
Why
Raw JSON is verbose for LLM input, especially for arrays of objects.
[
{ "name": "John", "age": 30, "city": "New York" },
{ "name": "Jane", "city": "Boston" }
]With llm-json-compact:
[["name","age","city"],["John",30,"New York"],["Jane",,"Boston"]]Install
npm install llm-json-compactQuick Start
import { jsonCompact, stringifyCompact } from "llm-json-compact";
const compacted = jsonCompact([
{ name: "John", age: 30, city: "New York" },
{ name: "Jane", city: "Boston" },
]);
console.log(compacted);
// [
// ["name", "age", "city"],
// ["John", 30, "New York"],
// ["Jane", undefined, "Boston"]
// ]
console.log(stringifyCompact(compacted));
// [["name","age","city"],["John",30,"New York"],["Jane",,"Boston"]]API
jsonCompact(input)
Compacts a plain object or an array of plain objects into a nested array structure.
stringifyCompact(compacted)
Serializes compacted output into a shorter string form for LLM prompts.
Rules:
- Middle
undefinedvalues are rendered as empty slots:["Jane",,"Boston"] - Trailing
undefinedvalues are omitted nullstaysnull
Output Rules
- A plain object becomes a two-row table: headers + values
- An array of objects becomes one header row plus one row per object
- A nested object becomes a single compact column like
address[city,zip] - An array of objects inside a field becomes a compact column like
addresses[city,street] - Scalar arrays are kept as-is
- New keys discovered in later rows are appended to the header row
Examples
Plain object
jsonCompact({
name: "John",
age: 30,
city: "New York",
});
// [
// ["name", "age", "city"],
// ["John", 30, "New York"]
// ]Nested object
jsonCompact({
name: "John",
age: 30,
address: {
city: "New York",
zip: "10001",
},
});
// [
// ["name", "age", "address[city,zip]"],
// ["John", 30, ["New York", "10001"]]
// ]Array of objects
jsonCompact([
{ name: "John", age: 30, hobbies: ["reading", "traveling"] },
{ name: "Jane", age: 25, hobbies: ["cooking"], city: "New York" },
]);
// [
// ["name", "age", "hobbies", "city"],
// ["John", 30, ["reading", "traveling"]],
// ["Jane", 25, ["cooking"], "New York"]
// ]Nested array of objects
jsonCompact({
name: "John",
age: 30,
addresses: [
{ city: "New York", street: "1225 Hanover Street" },
{ city: "Los Angeles", street: "456 Sunset Boulevard" },
],
});
// [
// ["name", "age", "addresses[city,street]"],
// [
// "John",
// 30,
// [
// ["New York", "1225 Hanover Street"],
// ["Los Angeles", "456 Sunset Boulevard"]
// ]
// ]
// ]Limitations
- Input must be a plain object or an array of plain objects
- Empty arrays are inferred with a small heuristic, so field naming can affect whether an empty array is treated as scalar or object-like
- The format is intentionally compact, not strict JSON schema
- This is optimized for LLM consumption, not lossless typed serialization
Development
npm test
npm run build