Registry / database / json-criteria

json-criteria

JSON →
library2.6.0jsnpmunverified

A JavaScript library for evaluating MongoDB-style query criteria on JSON objects. Version 2.6.0 is the latest stable. It supports common MongoDB operators like $and, $or, $gt, $lt, $in, $regex, $elemMatch, etc. Useful for in-memory filtering and testing. The library is small, has no dependencies, and works in Node.js and modern browsers. Differentiator: a lightweight alternative to MongoDB query evaluation for non-database contexts.

npm install json-criteria
INSTALL
IMPORT
SIG · JSON-CRITERIA
J
json-criteria
databasejavascriptv2.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

test
import { test } from 'json-criteria'
const test = require('json-criteria').test
ESM import works in Node.js with type:module or bundler; CommonJS require also supported.
filter
import { filter } from 'json-criteria'
Also exported as a named function; can be used with ESM or CJS.
jsonCriteria
import jsonCriteria from 'json-criteria'
import * as jsonCriteria from 'json-criteria'
Default export is not documented; prefer named exports for clarity.
JC
const JC = require('json-criteria')
import JC from 'json-criteria'
CommonJS style returns an object with test and filter methods; ESM default is not the same object.

Shows import, test, filter usage with MongoDB-style operators including $gt, $gte, $in, $and.

import { test, filter } from 'json-criteria'; // Test a single object against a criteria const obj = { name: 'Alice', age: 30 }; const criteria = { age: { $gt: 25 } }; console.log(test(obj, criteria)); // true // Filter an array of objects const items = [ { name: 'Alice', price: 10 }, { name: 'Bob', price: 5 }, { name: 'Charlie', price: 15 } ]; const filtered = filter(items, { price: { $gte: 10 } }); console.log(filtered); // [{ name: 'Alice', price: 10 }, { name: 'Charlie', price: 15 }] // Complex query with $and const complexCriteria = { $and: [ { age: { $gte: 18 } }, { status: { $in: ['active', 'pending'] } } ] }; const user = { age: 25, status: 'active' }; console.log(test(user, complexCriteria)); // true
Debug
Known issues
deprecatedThe project has not been updated since 2016; does not support newer JavaScript features or TypeScript natively.
fix
Consider using a maintained alternative or fork if needed.
affects: >=2.0.0
gotchaThe $where operator only supports functions, not strings. Using a string throws an error.
fix
Provide a function instead of a string for $where.
affects: >=1.0.0
gotchaIn Node.js, installing babel/register is required for ES6+ syntax as per the documentation. This may conflict with modern Node.js versions.
fix
Use native ES modules or update the code to avoid babel.
affects: >=2.0.0
gotchaThe library does not provide TypeScript definitions; if using TypeScript, you must declare module manually.
fix
Add a declaration file or use require with @ts-ignore.
affects: *
Errors
Common errors & fixes
TypeError: jsonCriteria.test is not a function
Using default import when the default export is an object, but named imports are different.
fix
Use named import: import { test } from 'json-criteria'
ReferenceError: require is not defined in ES module scope
Using require() in an ESM context without proper configuration.
fix
Switch to import statements or use createRequire.
Error: $where requires a function
Passing a string to $where instead of a function.
fix
Use a function: { $where: function(v) { return v > 0; } }
Upgrade
Version history
2.6.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources
json-criteria — npm install json-criteria · libregistry