The project is in a healthy, maintained state
An extension over JsonLogic that translates Rules to human text via a mixin and a small Rule wrapper.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

json-logic-rb-humanizable

Translate JsonLogic rules into readable sentences. Extension for JsonLogic.

โš™๏ธ Zero deps ยท ๐Ÿ› ๏ธ Configurable ยท ๐Ÿงฉ Mixin + Wrapper

Gem Version Docs License: MIT

What

This gem converts JsonLogic Rules into readable sentences.

  • A mixin for serializer/presenter classes.
  • A small wrapper for one-off translations.

No dependencies. Works with a Ruby Hash.

How

Config => operators map + variables map Input => JsonLogic Rule (Hash) Output => human-readable sentence

Where

If you found this gem, you likely already know where to use it in your app โ€” the abstract use case is simple: you want to read a Rule as text (e.g., show it in the UI, preview it, or share it elsewhere).

Installation

Plain Ruby

gem install json-logic-rb-humanizable

Rails (Bundler)

Add to your Gemfile if you need:

gem "json-logic-rb-humanizable"

Then install:

bundle install

Configuration (if needed)

# config/initializers/json_logic.rb

require "json_logic"

# Configuration

Examples

Easy โ€“ Quick Start

logic = {
  "and" => [
    { ">=" => [ { "var" => "payment.amount" }, 50 ] },
    { "in" => [ { "var" => "payment.currency" }, %w[EUR USD] ] }
  ]
}

puts JsonLogic::Rule.new(logic).humanize

Example output:

payment.amount is greater than or equal to 50 AND payment.currency is in EUR, USD

Complex โ€“ With Config

JsonLogic::Config.operators["in"] = "is one of"

JsonLogic::Config.vars[/([^\.]+)$/] =  ->(m) { m[1].tr("_", " ").split.map(&:capitalize).join(" ") }

logic = {
  "and" => [
    { ">=" => [ { "var" => "payment.amount" }, 50 ] },
    { "fact" => "payment.currency", "operator" => "in", "value" => %w[EUR USD] }
  ]
}

puts JsonLogic::Rule.new(logic).humanize

Example output:

Amount is greater than or equal to 50 AND Currency is one of ["EUR", "USD"]

Mixin โ€“ Humanizable in serializers

class RuleSerializer
  include JsonLogic::Humanizable

  attr_reader :json_logic

  def initialize(json_logic)
    @json_logic = json_logic
  end

   def condition
      humanize_logic(json_logic)
   end
end

s = RuleSerializer.new({
  "and" => [
    { ">=" => [ { "var" => "payment.amount" }, 50 ] },
    { "in" => [ { "var" => "payment.currency" }, %w[EUR USD] ] }
  ]
})

puts s.condition

Example output:

payment.amount is greater than or equal to 50 AND payment.currency is in EUR, USD

Configuration

Operators mapping

Configure labels as you prefer:

JsonLogic::Config.operators["in"]  = "is one of"
JsonLogic::Config.operators["=="]  = "equals"
JsonLogic::Config.operators[">="]  = "is at least"
JsonLogic::Config.operators["!"]   = "NOT"
JsonLogic::Config.operators["and"] = "AND"
JsonLogic::Config.operators["or"]  = "OR"

Variables mapping

Each entry is key โ€“ pattern, value โ€“ replacement, where pattern is a Regexp or exact String, and replacement is a String or Proc.

JsonLogic::Config.vars[/([^\.]+)$/] =  ->(m) { m[1].tr("_", " ").split.map(&:capitalize).join(" ") }

This maps payment.amount โ†’ Amount, customer.country โ†’ Country, etc.

Links