JSON Logic Qubit Python Parser
json-logic-qubit is a Python parser that accepts JsonLogic rules and executes them. It is a port of the JavaScript json-logic-js project, designed to allow sharing rules (logic) between front-end and back-end code, and for storing logic in databases. The current version is 0.9.1. The project appears to be in maintenance mode with infrequent updates.
Warnings
- deprecated The json-logic-qubit library has not been updated since August 2018. While functional, it may lack compatibility with newer Python versions or not include features/bug fixes from the broader JsonLogic ecosystem. Related projects by 'QubitProducts' have been archived.
- gotcha An older, less maintained Python package named `json-logic` exists on PyPI. Installing it by mistake can lead to an outdated and buggy implementation of the JsonLogic specification.
- gotcha Older versions of a base `json-logic` library (from which this project is forked) experienced `TypeError: 'dict_keys' object does not support indexing` with Python 3.6+. While potentially fixed in this fork, due to the project's age, unforeseen compatibility issues with modern Python versions (3.7+) may arise.
- gotcha Some JsonLogic implementations (in other languages) have had a known issue where the `{"var": "key"}` operator returns `None` instead of `False` when the referenced data value is literally `False`. This can break logic relying on explicit boolean `False` checks.
Install
-
pip install json-logic-qubit
Imports
- jsonLogic
from json_logic_qubit import jsonLogic
from json_logic import jsonLogic
Quickstart
from json_logic import jsonLogic
# Simple rule: 1 == 1
result1 = jsonLogic({ "==" : [1, 1] })
print(f"Simple rule (1 == 1): {result1}")
# Data-driven rule: check if 'temp' < 110 AND 'pie.filling' == 'apple'
rules = {
"and" : [
{ "<" : [ { "var" : "temp" }, 110 ] },
{ "==" : [ { "var" : "pie.filling" }, "apple" ] }
]
}
data = { "temp" : 100, "pie" : { "filling" : "apple" } }
result2 = jsonLogic(rules, data)
print(f"Complex rule (pie ready): {result2}")