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 is a Ruby gem that implements the
JSON Logic specification
with up-to-date operators. It's 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_json_logic is the only Ruby gem that evaluates JSON Logic rules exactly as its counterparts in languages like JS or Python and is also the fastest one by far in the Ruby ecosystem.
See the benchmarks or the comparison on the homepage.
shiny_json_logic
as the engine in the examples. There's no guarantee other implementations implement the same features
or follow the standard correctly.