jitson
raw JSON → 1.0.0 verified Fri May 01 auth: no javascript
Just-In-Time JSON.parse compiler that optimizes parsing by schema sampling. Version 1.0.0 is stable. It samples incoming JSON data and if a stable schema is detected, compiles a fast parser using turbo-json-parse. Differentiators: JIT compilation based on runtime schema analysis, supports Node.js buffers for faster parsing. Maintained by mafintosh. Release cadence is low; no recent updates.
Common errors
error TypeError: jitson is not a constructor ↓
cause Using 'new jitson()' instead of calling jitson() as a function.
fix
const parse = jitson(); // No 'new' keyword
error TypeError: parse.schema is not an object ↓
cause Accessing parse.schema before any parsing has been done.
fix
Call parse() at least once before checking parse.schema. After schema stabilization it will be an object.
error Error: Unable to compile parser - schema too complex ↓
cause The JSON schema is too complex for turbo-json-parse to compile.
fix
Simplify the JSON data structure or fall back to JSON.parse.
Warnings
gotcha jitson is not a class; do not use 'new'. ↓
fix Call jitson() directly (const parse = jitson()).
gotcha The parse function expects a string or Buffer, not a parsed object. ↓
fix Pass JSON.stringify output to parse, not JSON.parse output.
gotcha The underlying turbo-json-parse may fail for very complex or nested schemas. ↓
fix Fall back to JSON.parse manually if jitson throws.
gotcha Schema detection works best when each endpoint uses a separate jitson instance. ↓
fix Create one jitson instance per API endpoint or data source.
Install
npm install jitson yarn add jitson pnpm add jitson Imports
- default (jitson function) wrong
const jitson = require('jitson')correctimport jitson from 'jitson' - jitson constructor wrong
const parse = new jitson()correctconst parse = jitson() - parse function wrong
parse(JSON.parse(jsonString))correctconst obj = parse(jsonString) - parse.schema
const schema = parse.schema
Quickstart
import jitson from 'jitson';
const parse = jitson({ sampleInterval: 10 });
for (let i = 0; i < 10; i++) {
const obj = parse(JSON.stringify({ hello: 'world', number: Math.random() }));
console.log(obj);
}
console.log('Optimized schema:', parse.schema);