JSON-Logic

0.6.3 · active · verified Fri Apr 10

json-logic-py is a Python implementation of JsonLogic, a lightweight JSON-based rules engine. It allows users to build complex logic rules using a JSON structure, serialize them, and execute them in Python to evaluate data. The current version is 0.6.3, and it appears to be in a stable maintenance phase with infrequent updates, requiring Python 3.6+.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a JsonLogic rule using a Python dictionary and evaluate it against a data context. It shows a basic boolean evaluation and an example using the 'var' operator to access data.

from json_logic import jsonLogic

rules = {
    "and": [
        {"<": [1, 2]},
        {">": [1, 0]}
    ]
}

data = {}
result = jsonLogic(rules, data)
print(f"Result: {result}")

# Example with actual data
user_data = {"temp": 100, "humidity": 50}
rules_with_var = {
    "and": [
        {"<": [{"var": "temp"}, 150]},
        {"<=": [{"var": "humidity"}, 60]}
    ]
}
result_with_data = jsonLogic(rules_with_var, user_data)
print(f"Result with data: {result_with_data}")

view raw JSON →