Registry / serialization / json-logic

json-logic

JSON →
library0.6.3pypypi✓ verified 24d ago

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+.

pip install json-logic
INSTALL
IMPORT
SIG · JSON-LOGIC
J
json-logic
serializationpythonv0.6.3
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

jsonLogic
from json_logic import jsonLogic
The PyPI package name is 'json-logic', but the module to import is 'json_logic' (note the underscore instead of hyphen).

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}")
Debug
Known issues
breakingThe behavior of the `in` operator changed in version 0.6.0. Previously, it might have checked for substring presence in a string. Since 0.6.0, `in` strictly checks for element presence within a list.
fix
Review existing rules that use the `in` operator and ensure they are intended to operate on lists. If string substring checking is needed, implement it with custom logic or different operators.
affects: 0.6.0 and higher
gotchaJsonLogic rules must strictly adhere to the defined JSON structure, where operator keys are always single keys in an object, and their values are arrays of arguments. Deviations will lead to errors or unexpected behavior.
fix
Always consult the official JsonLogic specification or examples to ensure correct rule syntax, especially for complex or nested conditions.
affects: All versions
gotchaWhen receiving JsonLogic rules from untrusted sources (e.g., user input), there's a potential for logic bombs or resource exhaustion if highly complex or deeply nested rules are submitted. While the library itself doesn't execute arbitrary code, poorly constructed rules can still impact performance.
fix
Sanitize or validate incoming rules. Consider implementing limits on rule complexity (e.g., nesting depth, number of operations) if rules are user-provided.
affects: All versions
gotchaCustom operators must be explicitly registered with the `add_operation` function before they can be used in your rules. They are not automatically discovered.
fix
Ensure `json_logic.add_operation('my_operator', my_function)` is called for each custom operator before evaluating any rules that use it.
affects: All versions
Errors
Common errors & fixes
TypeError: 'dict_keys' object does not support indexing
This error occurs in Python 3.x when the library attempts to directly access elements of a `dict_keys` object (returned by `dict.keys()`) using an index, as `dict_keys` is an iterable view and not subscriptable like a list. This was a known issue in older versions of the `json-logic-py` library (version 0.6.3 specifically).
fix
Upgrade the `json-logic` library to a newer version or a compatible fork that has addressed this Python 3 incompatibility, or convert the `dict_keys` object to a list before indexing if modifying the library's source.
NameError: name 'jsonLogic' is not defined
The `jsonLogic` function, which is the main entry point for evaluating rules, was called without being properly imported from the `json_logic` module.
fix
Add `from json_logic import jsonLogic` at the beginning of your Python script or module to import the function.
ModuleNotFoundError: No module named 'json_logic'
The `json-logic` package (which provides the `json_logic` module) is not installed in the current Python environment, or there is a typo in the import statement.
fix
Install the library using pip: `pip install json-logic` or `pip install json-logic-py` (depending on the project's pypi name, `json-logic` is the correct one for nadirizr's project). Correct the import statement if there's a typo.
Unrecognized operation %s
The JSON logic rule provided contains an operator that is not recognized or supported by the `json-logic-py` library's default operations.
fix
Check the spelling of the operator in your JSON rule against the list of supported operators (e.g., from jsonlogic.com or the library's documentation). If it's a custom operator, ensure it has been correctly added to the operations registry.
Upgrade
Version history
0.6.3latest on PyPI · released Dec 4, 2015
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
json-logic — pip install json-logic · libregistry