{"id":13399,"library":"json-logic-js","title":"JSON Logic JavaScript Interpreter","description":"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.","status":"maintenance","version":"2.0.5","language":"javascript","source_language":"en","source_url":"https://github.com/jwadhams/json-logic-js","tags":["javascript","json","logic","jsonlogic","rules"],"install":[{"cmd":"npm install json-logic-js","lang":"bash","label":"npm"},{"cmd":"yarn add json-logic-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add json-logic-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { jsonLogic } from 'json-logic-js';","symbol":"jsonLogic","correct":"import jsonLogic from 'json-logic-js';"},{"note":"For CommonJS environments, `require()` directly returns the `jsonLogic` object, which has the `apply` method.","symbol":"jsonLogic","correct":"const jsonLogic = require('json-logic-js');"},{"note":"TypeScript types are provided by the `@types/json-logic-js` package. The primary type for rules is `RulesLogic`.","wrong":"import { JsonLogicRules } from 'json-logic-js';","symbol":"JsonLogicRules","correct":"import type { RulesLogic } from 'json-logic-js';"}],"quickstart":{"code":"import jsonLogic from 'json-logic-js';\n\n// Define a complex rule combining logical AND, comparison, and data access\nconst rules = {\n  \"and\": [\n    {\">\": [{\"var\": \"temp\"}, 110]},\n    {\"===\": [{\"var\": \"pie.filling\"}, \"apple\"]}\n  ]\n};\n\n// Define data to evaluate against the rules\nconst dataReady = {\"temp\": 100, \"pie\": {\"filling\": \"apple\"}};\nconst dataNotReadyTemp = {\"temp\": 120, \"pie\": {\"filling\": \"apple\"}};\nconst dataNotReadyFilling = {\"temp\": 100, \"pie\": {\"filling\": \"cherry\"}};\n\n// Apply the rules to different data sets\nconst resultReady = jsonLogic.apply(rules, dataReady);\nconst resultNotReadyTemp = jsonLogic.apply(rules, dataNotReadyTemp);\nconst resultNotReadyFilling = jsonLogic.apply(rules, dataNotReadyFilling);\n\nconsole.log('Is pie ready (temp 100, filling apple)?', resultReady); // Expected: false (corrected logic based on example implies < 110)\nconsole.log('Is pie ready (temp 120, filling apple)?', resultNotReadyTemp); // Expected: false\nconsole.log('Is pie ready (temp 100, filling cherry)?', resultNotReadyFilling); // Expected: false\n\n// Corrected rule from README for 'cooler than 110' implies '<'\nconst correctedRules = { \"and\" : [\n  {\"<\": [ { \"var\" : \"temp\" }, 110 ]},\n  {\"===\": [ { \"var\" : \"pie.filling\" }, \"apple\" ] }\n] };\nconst correctedResultReady = jsonLogic.apply(correctedRules, dataReady);\nconsole.log('Is pie ready (corrected logic)?', correctedResultReady); // Expected: true","lang":"javascript","description":"Demonstrates defining and applying a compound JSON Logic rule with dynamic data, showcasing comparison and nested object access."},"warnings":[{"fix":"Migrate package management from Bower to npm or Yarn using `npm install json-logic-js` or `yarn add json-logic-js`.","message":"Starting 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.","severity":"breaking","affected_versions":">=2.0.5"},{"fix":"Include polyfills for `Array.map` and `Array.reduce` (e.g., via Babel or directly from MDN) if supporting legacy browsers.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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');`).","cause":"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.","error":"TypeError: jsonLogic.apply is not a function"},{"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.","cause":"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.","error":"ReferenceError: jsonLogic is not defined"},{"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).","cause":"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.","error":"TS2345: Argument of type 'string' is not assignable to parameter of type 'RulesLogic<...>'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}