Build complex rules, serialize them as JSON, share them between front-end and back-end

Table of Contents generated with DocToc

Accessing Data


Retrieve data from the provided data object.

Most JsonLogic rules operate on data supplied at run-time. Typically this data is an object, in which case the argument to var is a property name.

Note, every operation will be demonstrated with a live example box. Feel free to edit the logic and the data and see what happens when you apply your change! Here’s what the example above would look like in JavaScript:

  { "var" : ["a"] }, // Logic
  { "a":1, "b":2 }   // Data
// 1

If you like, we support syntactic sugar to skip the array around single arguments :

You can supply a default, as the second argument, for values that might be missing in the data object. (Note, the skip-the-array sugar won’t work here because you’re passing two arguments to var):

The key passed to var can use dot-notation to get the property of a property (to any depth you need):

You can also use the var operator to access an array by numeric index:

Here’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.


Takes an array of data keys to search for (same format as var). Returns an array of any keys that are missing from the data object, or an empty array.

Note, in JsonLogic, empty arrays are falsy. So you can use missing with if like:


Takes a minimum number of data keys that are required, and an array of keys to search for (same format as var or missing). Returns an empty array if the minimum is met, or an array of the missing keys otherwise.

This is useful if you’re using missing to track required fields, but occasionally need to require N of M fields.

Logic and Boolean Operations


The if statement typically takes 3 arguments: a condition (if), what to do if it’s true (then), and what to do if it’s false (else), like:

If can also take more than 3 arguments, and will pair up arguments like if/then elseif/then elseif/then else. Like:

See the Fizz Buzz implementation for a larger example.


Tests equality, with type coercion. Requires two arguments.


Tests strict equality. Requires two arguments.


Tests not-equal, with type coercion.


Tests strict not-equal.


Logical negation (“not”). Takes just one argument.

Note: unary operators can also take a single, non array argument:


or can be used for simple boolean tests, with 1 or more arguments.

At a more sophisticated level, or returns the first truthy argument, or the last argument.


and can be used for simple boolean tests, with 1 or more arguments.

At a more sophisticated level, and returns the first falsy argument, or the last argument.

Numeric Operations

>, >=, <, and <=

Greater than:

Greater than or equal to:

Less than:

Less than or equal to:


You can use a special case of < and <= to test that one value is between two others:

Between exclusive:

Between inclusive:

This is most useful with data:

max and min

Return the maximum or minimum from a list of values.

Arithmetic, + - * /

Addition, subtraction, multiplication, and division.

Because addition and multiplication are associative, they happily take as many args as you want:

Passing just one argument to - returns its arithmetic negative (additive inverse).

Passing just one argument to + casts it to a number.


Modulo. Finds the remainder after the first argument is divided by the second argument.

This can be paired with a loop in the language that parses JsonLogic to create stripes or other effects.

In Javascript:

var rule = {"if": [{"%": [{"var":"i"}, 2]}, "odd", "even"]};
for(var i = 1; i <= 4 ; i++){
  console.log(i, jsonLogic.apply(rule, {"i":i}));
/* Outputs:
1 "odd"
2 "even"
3 "odd"
4 "even"

Array Operations


Takes one or more arrays, and merges them into one array. If arguments aren’t arrays, they get cast to arrays.

Merge can be especially useful when defining complex missing rules, like which fields are required in a document. For example, this vehicle paperwork always requires the car’s VIN, but only needs the APR and term if you’re financing.


If the second argument is an array, tests that the first argument is a member of the array:

String Operations


If the second argument is a string, tests that the first argument is a substring:


Concatenate all the supplied arguments. Note that this is not a join or implode operation, there is no “glue” string.



Logs the first value to console, then passes it through unmodified.

This can be especially helpful when debugging a large rule.

(Check your developer console!)