The fastest AND most compliant JSONLogic implementation for Ruby. β¨
ShinyJsonLogic is a pure Ruby, zero-dependency JSON Logic implementation β the only Ruby gem that passes 100% of the official JSON Logic tests, and faster than all competitors on every Ruby version tested.
If you've used JSON Logic in Ruby before, you've probably found that other gems don't behave like their JS or Python counterparts: It's either wrong outputs on edge cases, missing operators or subtle bugs... And you end up patching, spawning a JS process or writing the interpreter yourself. Been there.
ShinyJsonLogic fixes that:
- π Fastest Ruby JSON Logic gem β wins across all Ruby versions tested, up to +117% faster than competitors.
- β 100% spec-compliant β the only Ruby gem that passes all official JSON Logic tests.
- π§© Zero runtime dependencies (stdlib-only). Just plug & play.
- π°οΈ Ruby 2.4+ compatible β one of the lowest minimum versions in the Ruby ecosystem.
- π§ Actively maintained and continuously improved.
Try it in the sandbox at jsonlogicruby.com/playground, or see the full benchmark results at jsonlogicruby.com/benchmarks.
Add it to your Gemfile:
gem "shiny_json_logic"Then run:
bundle installOr install it directly:
gem install shiny_json_logicand require it in your project:
require "shiny_json_logic"Benchmarked via jsonlogic_benchmarks β automated CI on Linux across all Ruby versions (2.7 through 4.0, with and without YJIT). shiny_json_logic wins across the board, up to +117% faster than the next best gem.
See jsonlogicruby.com/benchmarks for the full interactive results.
If you're currently using json-logic-ruby, migration is seamless.
ShinyJsonLogic provides JsonLogic and JSONLogic as aliases, so you only need to swap the gem in your Gemfile:
- gem "json-logic-ruby"
+ gem "shiny_json_logic"Your existing code will work without changes:
require "shiny_json_logic"
# Both of these work exactly as before:
JsonLogic.apply(rule, data)
JSONLogic.apply(rule, data)
# Or use the new module name:
ShinyJsonLogic.apply(rule, data)Basic usage is intentionally simple:
require "shiny_json_logic"
rule = {
"==" => [
{ "var" => "status" },
"active"
]
}
data = { "status" => "active" }
ShinyJsonLogic.apply(rule, data)
# => trueRules can be nested arbitrarily:
rule = {
"if" => [
{ "var" => "financing" },
{ "missing" => ["apr"] },
[]
]
}
data = { "financing" => true }
ShinyJsonLogic.apply(rule, data)
# => ["apr"]Our goal is full JSON Logic coverage.
Currently implemented operators include:
if, and, or, !, !!, ?:, tryβ¨, throwβ¨
==, ===, !=, !==, >, >=, <, <=
var, missing, missing_some, valβ¨, existsβ¨
+, -, *, /, %, min, max
cat, substr
merge, in, ??β¨ (coalesce operator)
map, reduce, filter, some, all, none
preserveβ¨
See the spec for the full list of operators and their behavior.
ShinyJsonLogic uses native Ruby exceptions for error handling:
# Unknown operators raise an error
ShinyJsonLogic.apply({ "unknown_op" => [1, 2] }, {})
# => raises ShinyJsonLogic::Errors::UnknownOperator
# Invalid arguments raise an error
ShinyJsonLogic.apply({ "+" => ["not", "numbers"] }, {})
# => raises ShinyJsonLogic::Errors::InvalidArguments
# You can use try/throw for controlled error handling within rules
rule = {
"try" => [
{ "throw" => "Something went wrong" },
{ "cat" => ["Error: ", { "var" => "type" }] }
]
}
ShinyJsonLogic.apply(rule, {})
# => "Error: Something went wrong"Error classes:
ShinyJsonLogic::Errors::UnknownOperator- Unknown operator in ruleShinyJsonLogic::Errors::InvalidArguments- Invalid arguments to operatorShinyJsonLogic::Errors::NotANumber- NaN result in numeric operation
or rescue the ShinyJsonLogic::Errors::Base error class in a single sweep.
After checking out the repo:
bin/setup
bundle exec rspecOpen a console:
bin/consoleInstall locally:
bundle installHow to run the compatibility test suite:
bin/test.shContributions are welcome β especially:
- spec alignment improvements
- missing operators
- edge-case tests
- performance improvements
Please include tests with any change.
Repository:
https://github.com/luismoyano/shiny_json_logic
MIT License.
Use it. Fork it. Ship it. (:
Shine bright like a Ruby π