JSONLogic Ruby Shiny JSON Logic

The JSON Logic Specification

A portable format for encoding business rules as JSON objects. Evaluate the same logic across any language or platform.

What is the JSON Logic Specification?

The JSON Logic specification defines a standard way to encode business rules and conditional logic as JSON objects. Created by Jeremy Wadhams, the specification provides a portable, language-agnostic format that can be stored in databases, configuration files or APIs and transmitted between applications. They should be evaluated consistently across different programming languages by community maintained engines (like shiny_json_logic).

Unlike embedded code, JSON Logic rules are pure data; they can be safely stored, versioned, and evaluated.

Core Principles of the Specification

The JSON Logic specification is built on three fundamental principles:

1. Rules as JSON Objects

Every JSON Logic rule is a JSON object with a single key (the operator) and a value (the arguments):

rule_structure.json
{ "operator": [argument1, argument2, ...] }

For example, to check if a number is greater than 10:

example.json
{ ">": [{ "var": "score" }, 10] }

2. Evaluation Against Data

Rules are evaluated against a data object. The var operator retrieves values from this data, allowing rules to be dynamic and context-aware:

evaluation.json
// Rule
{ ">": [{ "var": "user.age" }, 18] }

// Data
{ "user": { "age": 25, "name": "Alice" } }

// Result
true

3. Composability Through Nesting

Any argument can itself be another rule, enabling complex logic from simple building blocks:

nested_rule.json
// Check if user is adult AND verified
{
  "and": [
    { ">": [{ "var": "age" }, 18] },
    { "==": [{ "var": "verified" }, true] }
  ]
}

Operators Defined by the Specification

The JSON Logic specification defines a comprehensive set of operators organized into categories:

Data Access

Retrieve values from the data object

var val missing missing_some

Logical Operators

Boolean logic with short-circuit evaluation

and or ! !! ??

Comparison

Compare values with optional type coercion

== === != !== > >= < <=

Arithmetic

Mathematical operations

+ - * / % min max

String Operations

Text manipulation

cat substr in

Array Operations

Transform and query collections

map filter reduce all some none merge

Control Flow

Conditional branching

if ?:

Error Handling

Exception management

try throw

Truthiness in the Specification

JSON Logic uses specific rules to determine if a value is considered "truthy" (treated as true) or "falsy" (treated as false). These rules are important for logical operators like and, or, if, and !.

Falsy Values

  • false — the boolean false
  • null — null/nil value
  • 0 — the number zero
  • "" — empty string
  • [] — empty array
  • {} — empty object

Truthy Values

  • true — the boolean true
  • Non-zero numbers (1, -5)
  • Non-empty strings ("hello")
  • Non-empty arrays ([1, 2])
  • Non-empty objects ({"a": 1})
Watch out for "0" and "false"
The strings "0" and "false" are truthy because they are non-empty strings. Only the empty string "" is falsy.
Practical usage

Truthiness is used by logical operators:

  • {"and": [a, b]} — returns first falsy value, or last value
  • {"or": [a, b]} — returns first truthy value, or last value
  • {"if": [cond, then, else]} — evaluates then if cond is truthy
  • {"!": val} — returns true if val is falsy

Cross-Language Compatibility

One of the key benefits of the JSON Logic specification is portability. The same rule can be evaluated in different programming languages with consistent results. These implementations are actively maintained and highly compliant:

See the full list of implementations at JSON Logic Compatibility Tables .

Common Use Cases

The JSON Logic specification enables powerful use cases where business rules need to be dynamic, stored, or shared:

Feature Flags

Control feature rollouts based on user attributes, percentages, or complex conditions without deploying code.

Access Control

Define dynamic permissions based on user roles, resource ownership, time windows, or any combination of factors.

Form Validation

Store validation rules alongside form definitions, enabling dynamic forms with complex conditional requirements.

Dynamic Pricing

Calculate prices based on quantity, customer tier, promotions, time-based discounts, and market conditions.

Eligibility Rules

Determine if users qualify for loans, insurance, benefits, or services based on configurable criteria.

Content Targeting

Show different content to users based on location, behavior, preferences, or A/B test groups.

Resources

Legacy

The original specification by Jeremy Wadhams:

Community

Active development and cross-implementation testing:

Ready to implement JSON Logic in Ruby?

shiny_json_logic is the only Ruby implementation that passes 100% of official JSON Logic tests, with modern operators.