JSON Logic is a format for encoding business rules as JSON objects. It provides a portable, language-agnostic way to define logic that can be evaluated consistently across different platforms—whether in Ruby on the server, JavaScript in the browser, or any other language with a JSON Logic implementation.
Structure
A JSON Logic rule is a JSON object with a single key (the operator) and a value (the arguments):
{ "operator": [argument1, argument2, ...] }
Rules can be nested—any argument can itself be another rule, allowing you to build complex expressions from simple primitives.
Data
Rules are evaluated against a data object. Use the val
operator to access values from this data:
Shiny JSON Logic
shiny_json_logic (Ruby) and shiny/json-logic-php (PHP)
are Shiny's implementations of the
JSON Logic specification
with up-to-date operators. Designed to be safe, simple and easy to use.
require 'shiny_json_logic'
rule = { ">" => [{ "val" => "age" }, 18] }
data = { "age" => 25 }
ShinyJsonLogic.apply(rule, data) # => true
# Aliases for easier migration from other gems
JsonLogic.apply(rule, data) # => true
JSONLogic.apply(rule, data) # => true
Why Shiny JSON Logic?
Shiny is the only family of JSON Logic libraries that evaluates rules exactly as their counterparts in languages like JS or Python. See the comparison on the homepage.
shiny_json_logic
(Ruby) as the engine in the interactive examples. The same operators apply to shiny/json-logic-php.
There's no guarantee other implementations implement the same features
or follow the standard correctly.