json-logic-js
This parser accepts JsonLogic rules and executes them in JavaScript.
The 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, including examples of every supported operation and a place to try out rules in your browser.
The same format can also be executed in PHP by the library json-logic-php
Examples
Simple
jsonLogic.apply( { "==" : [1, 1] } );
// true
This is a simple test, equivalent to 1 == 1
. A few things about the format:
- The operator is always in the "key" position. There is only one key per JsonLogic rule.
- The values are typically an array.
- Each value can be a string, number, boolean, array (non-associative), or null
Compound
Here we're beginning to nest rules.
jsonLogic.apply(
{"and" : [
{ ">" : [3,1] },
{ "<" : [1,3] }
] }
);
// true