Registry / serialization / json-logic-js

json-logic-js

JSON →
library2.0.5jsnpmunverified

json-logic-js is a JavaScript parser and executor for JsonLogic rules, allowing developers to define complex business logic as JSON objects. This enables the sharing and persistence of rules across different programming languages (e.g., JavaScript and PHP) and environments (front-end, back-end, database storage). The library currently stands at version 2.0.5, with an infrequent release cadence focused on maintenance and bug fixes rather than rapid feature development. Its core differentiator is the adherence to the well-documented, language-agnostic JsonLogic format, which explicitly separates rules from data and avoids side effects, ensuring deterministic computation. Alternatives like `json-logic-engine` offer modern ESM/CJS support and performance optimizations, highlighting `json-logic-js`'s legacy UMD module format as a point of contrast.

npm install json-logic-js
INSTALL
IMPORT
SIG · JSON-LOGIC-JS
J
json-logic-js
serializationjavascriptv2.0.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

jsonLogic
import jsonLogic from 'json-logic-js';
import { jsonLogic } from 'json-logic-js';
The package uses a UMD module loader. In ESM environments, it's typically imported as a default export, where the `jsonLogic` object contains the `apply` method.
jsonLogic
const jsonLogic = require('json-logic-js');
For CommonJS environments, `require()` directly returns the `jsonLogic` object, which has the `apply` method.
JsonLogicRules
import type { RulesLogic } from 'json-logic-js';
import { JsonLogicRules } from 'json-logic-js';
TypeScript types are provided by the `@types/json-logic-js` package. The primary type for rules is `RulesLogic`.

Demonstrates defining and applying a compound JSON Logic rule with dynamic data, showcasing comparison and nested object access.

import jsonLogic from 'json-logic-js'; // Define a complex rule combining logical AND, comparison, and data access const rules = { "and": [ {">": [{"var": "temp"}, 110]}, {"===": [{"var": "pie.filling"}, "apple"]} ] }; // Define data to evaluate against the rules const dataReady = {"temp": 100, "pie": {"filling": "apple"}}; const dataNotReadyTemp = {"temp": 120, "pie": {"filling": "apple"}}; const dataNotReadyFilling = {"temp": 100, "pie": {"filling": "cherry"}}; // Apply the rules to different data sets const resultReady = jsonLogic.apply(rules, dataReady); const resultNotReadyTemp = jsonLogic.apply(rules, dataNotReadyTemp); const resultNotReadyFilling = jsonLogic.apply(rules, dataNotReadyFilling); console.log('Is pie ready (temp 100, filling apple)?', resultReady); // Expected: false (corrected logic based on example implies < 110) console.log('Is pie ready (temp 120, filling apple)?', resultNotReadyTemp); // Expected: false console.log('Is pie ready (temp 100, filling cherry)?', resultNotReadyFilling); // Expected: false // Corrected rule from README for 'cooler than 110' implies '<' const correctedRules = { "and" : [ {"<": [ { "var" : "temp" }, 110 ]}, {"===": [ { "var" : "pie.filling" }, "apple" ] } ] }; const correctedResultReady = jsonLogic.apply(correctedRules, dataReady); console.log('Is pie ready (corrected logic)?', correctedResultReady); // Expected: true
Debug
Known issues
breakingStarting with version 2.0.5, `json-logic-js` has officially ended support for Bower as a package manager. Projects relying on Bower for this library should migrate to npm or Yarn.
fix
Migrate package management from Bower to npm or Yarn using `npm install json-logic-js` or `yarn add json-logic-js`.
affects: >=2.0.5
gotchaThe library directly uses `Array.map` and `Array.reduce`, making it incompatible with older browsers like Internet Explorer 8. For broad browser compatibility, polyfills are necessary.
fix
Include polyfills for `Array.map` and `Array.reduce` (e.g., via Babel or directly from MDN) if supporting legacy browsers.
affects: All versions
gotchaThe `json-logic-js` package uses a UMD (Universal Module Definition) format, which can sometimes lead to unexpected import behavior in modern ESM (ECMAScript Modules) environments compared to dedicated ESM-first libraries. While `import jsonLogic from 'json-logic-js'` generally works, developers might encounter issues with bundlers or Node.js's module resolution depending on configuration.
fix
Ensure your project's build tooling (e.g., Webpack, Rollup) or Node.js environment is configured to correctly handle UMD packages. Consider using `createRequire` in some ESM contexts for CJS-like imports if issues arise, or evaluate modern alternatives that offer explicit ESM/CJS dual builds for better interoperability.
affects: All versions
Errors
Common errors & fixes
TypeError: jsonLogic.apply is not a function
This usually happens when the `jsonLogic` object is not imported or required correctly, or when it's imported as a named export from a UMD module that provides a default export.
fix
Ensure you are importing `jsonLogic` as a default export in ESM (`import jsonLogic from 'json-logic-js';`) or correctly assigning the `require()` result in CommonJS (`const jsonLogic = require('json-logic-js');`).
ReferenceError: jsonLogic is not defined
The `jsonLogic` variable was used without being imported or declared in the current scope. This typically occurs in environments where the library isn't globally available or imported using module syntax.
fix
Add `import jsonLogic from 'json-logic-js';` (ESM) or `const jsonLogic = require('json-logic-js');` (CommonJS) at the top of your file to make the `jsonLogic` object available.
TS2345: Argument of type 'string' is not assignable to parameter of type 'RulesLogic<...>'
This TypeScript error indicates that the structure of the JSON Logic rule object passed to `jsonLogic.apply` does not conform to the `RulesLogic` type definition, often due to incorrect operator syntax or missing operands.
fix
Review the JsonLogic rule definition against the official documentation at jsonlogic.com/operations.html and ensure it matches the `RulesLogic` interface from `@types/json-logic-js`. Pay attention to operator positions (key) and operand types (array or single value).
Upgrade
Version history
2.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
json-logic-js — npm install json-logic-js · libregistry