panzi-json-logic: JsonLogic and CertLogic Implementation

1.0.1 · maintenance · verified Tue Apr 14

panzi-json-logic is a Pure Python 3 implementation of JsonLogic and CertLogic. It aims to closely emulate the behavior of the JavaScript JsonLogic specification, including specific operator semantics. The library facilitates sharing declarative rules (logic) between frontend and backend applications, storing them in databases, and evaluating them reliably across different environments. It is currently at version 1.0.1 and appears to be in a maintenance phase, focusing on correctness and compliance with the JsonLogic standard.

Warnings

Install

Imports

Quickstart

Demonstrates basic rule evaluation, including a data-driven example and how to register a custom operation, such as the UTF-16 specific substring implementation.

from json_logic import jsonLogic

# Simple rule evaluation
rule_simple = { "==": [1, 1] }
result_simple = jsonLogic(rule_simple)
print(f"Simple rule (1 == 1): {result_simple}")

# Data-driven rule evaluation
rule_data = { ">": [{ "var": "temp" }, 100] }
data = { "temp": 105 }
result_data = jsonLogic(rule_data, data)
print(f"Data-driven rule (temp > 100 with temp=105): {result_data}")

# Custom operations (example: using UTF16_SUBSTR for JS-like substr behavior)
from json_logic.builtins import UTF16_SUBSTR
custom_operations = { 'substr_utf16': UTF16_SUBSTR }
rule_substr = { 'substr_utf16': ['hello world', 0, 5] }
result_substr = jsonLogic(rule_substr, operations=custom_operations)
print(f"Custom substr (hello world, 0, 5): {result_substr}")

view raw JSON →