{"id":13439406,"url":"https://github.com/jwadhams/json-logic-js","last_synced_at":"2025-05-14T08:07:22.409Z","repository":{"id":41117492,"uuid":"43779142","full_name":"jwadhams/json-logic-js","owner":"jwadhams","description":"Build complex rules, serialize them as JSON, and execute them in JavaScript","archived":false,"fork":false,"pushed_at":"2024-07-09T15:00:20.000Z","size":251,"stargazers_count":1322,"open_issues_count":80,"forks_count":143,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-09T03:40:00.852Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwadhams.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-06T21:15:03.000Z","updated_at":"2025-05-08T10:23:04.000Z","dependencies_parsed_at":"2024-12-17T22:00:59.568Z","dependency_job_id":null,"html_url":"https://github.com/jwadhams/json-logic-js","commit_stats":{"total_commits":93,"total_committers":10,"mean_commits":9.3,"dds":"0.17204301075268813","last_synced_commit":"9c805e9ac6a3787e8508e982a079888d3cc295b5"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwadhams%2Fjson-logic-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwadhams%2Fjson-logic-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwadhams%2Fjson-logic-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwadhams%2Fjson-logic-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwadhams","download_url":"https://codeload.github.com/jwadhams/json-logic-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101555,"owners_count":22014908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:13.625Z","updated_at":"2025-05-14T08:07:19.376Z","avatar_url":"https://github.com/jwadhams.png","language":"JavaScript","funding_links":[],"categories":["HarmonyOS","JavaScript"],"sub_categories":["Windows Manager"],"readme":"# json-logic-js\n\nThis parser accepts [JsonLogic](http://jsonlogic.com) rules and executes them in JavaScript.\n\nThe JsonLogic format is designed to allow you to share rules (logic) between front-end and back-end code (regardless of language difference), even to store logic along with a record in a database.  JsonLogic is documented extensively at [JsonLogic.com](http://jsonlogic.com), including examples of every [supported operation](http://jsonlogic.com/operations.html) and a place to [try out rules in your browser](http://jsonlogic.com/play.html).\n\nThe same format can also be executed in PHP by the library [json-logic-php](https://github.com/jwadhams/json-logic-php/)\n\n## Installation\n\nWe recommend that you install this library with a package manager, like [NPM](https://www.npmjs.com/) (or Yarn, etc):\n\n```bash\nnpm install json-logic-js\n```\n\nNote that this project uses a [module loader](http://ricostacruz.com/cheatsheets/umdjs.html) that also makes it suitable for RequireJS projects.\n\nIf that doesn't suit you, and you want to manage updates yourself, the entire library is self-contained in `logic.js` and you can download it straight into your project as you see fit.\n\n```bash\ncurl -O https://raw.githubusercontent.com/jwadhams/json-logic-js/master/logic.js\n```\n\n## Examples\n\n### Simple\n```js\njsonLogic.apply( { \"==\" : [1, 1] } );\n// true\n```\n\nThis is a simple test, equivalent to `1 == 1`.  A few things about the format:\n\n  1. The operator is always in the \"key\" position. There is only one key per JsonLogic rule.\n  1. The values are typically an array.\n  1. Each value can be a string, number, boolean, array (non-associative), or null\n\n### Compound\nHere we're beginning to nest rules.\n\n```js\njsonLogic.apply(\n  {\"and\" : [\n    { \"\u003e\" : [3,1] },\n    { \"\u003c\" : [1,3] }\n  ] }\n);\n// true\n```\n\nIn an infix language (like JavaScript) this could be written as:\n\n```js\n( (3 \u003e 1) \u0026\u0026 (1 \u003c 3) )\n```\n\n### Data-Driven\n\nObviously these rules aren't very interesting if they can only take static literal data. Typically `jsonLogic` will be called with a rule object and a data object. You can use the `var` operator to get attributes of the data object:\n\n```js\njsonLogic.apply(\n  { \"var\" : [\"a\"] }, // Rule\n  { a : 1, b : 2 }   // Data\n);\n// 1\n```\n\nIf you like, we support [syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) on unary operators to skip the array around values:\n\n```js\njsonLogic.apply(\n  { \"var\" : \"a\" },\n  { a : 1, b : 2 }\n);\n// 1\n```\n\nYou can also use the `var` operator to access an array by numeric index:\n\n```js\njsonLogic.apply(\n  {\"var\" : 1 },\n  [ \"apple\", \"banana\", \"carrot\" ]\n);\n// \"banana\"\n```\n\nHere's a complex rule that mixes literals and data. The pie isn't ready to eat unless it's cooler than 110 degrees, *and* filled with apples.\n\n```js\nvar rules = { \"and\" : [\n  {\"\u003c\" : [ { \"var\" : \"temp\" }, 110 ]},\n  {\"==\" : [ { \"var\" : \"pie.filling\" }, \"apple\" ] }\n] };\n\nvar data = { \"temp\" : 100, \"pie\" : { \"filling\" : \"apple\" } };\n\njsonLogic.apply(rules, data);\n// true\n```\n\n### Always and Never\nSometimes the rule you want to process is \"Always\" or \"Never.\"  If the first parameter passed to `jsonLogic` is a non-object, non-associative-array, it is returned immediately.\n\n```js\n//Always\njsonLogic.apply(true, data_will_be_ignored);\n// true\n\n//Never\njsonLogic.apply(false, i_wasnt_even_supposed_to_be_here);\n// false\n```\n\n## Compatibility\n\nThis library makes use of `Array.map` and `Array.reduce`, so it's not *exactly* Internet Explorer 8 friendly.\n\nIf you want to use JsonLogic *and* support deprecated browsers, you could easily use [BabelJS's polyfill](https://babeljs.io/docs/usage/polyfill/) or directly incorporate the polyfills documented on MDN for [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).\n\n## Customization\n\nIt's not possible to include everyone's excellent ideas without the core library bloating, bringing in a ton of outside dependencies, or occasionally causing use case conflicts (some people need to safely execute untrusted rules, some people need to change outside state).\n\nCheck out the [documentation for adding custom operations](http://jsonlogic.com/add_operation.html) and be sure to stop by the [Wiki page of custom operations](https://github.com/jwadhams/json-logic-js/wiki/Custom-Operations) to see if someone has already solved your problem or to share your solution.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwadhams%2Fjson-logic-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwadhams%2Fjson-logic-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwadhams%2Fjson-logic-js/lists"}