Registry / serialization / panzi-json-logic

panzi-json-logic

JSON →
library1.0.1pypypi✓ verified 93d ago

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.

pip install panzi-json-logic
INSTALL
IMPORT
SIG · PANZI-JSON-LOGIC
P
panzi-json-logic
serializationpythonv1.0.1
Install
1.5s avg
Import
43ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.0.1 · 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.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.047s · 17.9MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.040s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

jsonLogic
✓ from json_logic import jsonLogic
✗ import json_logic
The primary evaluation function `jsonLogic` is directly imported from the `json_logic` package.

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}")
Debug
Known issues
gotchaThe `substr` operator in `panzi-json-logic` operates on Unicode code points, which differs from the `json-logic-js` JavaScript implementation that operates on UTF-16 code units. This can lead to different string slicing behavior for certain non-ASCII strings.
fix
If strict JavaScript `substr` emulation is required, import and use `json_logic.builtins.UTF16_SUBSTR` as a custom operation: `from json_logic.builtins import UTF16_SUBSTR; jsonLogic(rule, operations={'substr': UTF16_SUBSTR})`.
affects: All versions
gotchaThe `==` operator attempts to emulate JavaScript's loose equality, which performs type coercion. This can lead to unexpected results if Python's strict `==` behavior is anticipated. For strict equality, use the `===` operator.
fix
Always use `===` for strict type-and-value equality comparisons to avoid implicit type coercion behavior.
affects: All versions
gotchaJsonLogic rules are standard JSON. Common JSON syntax errors (e.g., trailing commas, unquoted keys, single quotes instead of double quotes, incorrect casing for `true`/`false`/`null`) will cause parsing failures before the logic can be evaluated.
fix
Ensure all JsonLogic rule definitions strictly adhere to the JSON specification. Use a JSON linter or validator for complex rules.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'json_logic'
The `panzi-json-logic` package has not been installed or is not accessible in the current Python environment.
fix
Install the package using pip: `pip install panzi-json-logic`
TypeError: 'list' object has no attribute 'items'
The logic argument passed to `JsonLogic().evaluate()` is not a dictionary (e.g., a list or string), but the library expects a dictionary where the key is an operator.
fix
Ensure the logic passed to `evaluate()` is a dictionary, structured as `{"operator": [arguments, ...]}`.
KeyError: 'unknown_operator_name'
The logic dictionary contains an operator key that is not recognized or supported by the `panzi-json-logic` library, either due to a typo or an attempt to use a non-standard operator without defining it.
fix
Use one of the standard JsonLogic operators (e.g., `and`, `or`, `==`, `>`, `<`, `+`, `-`) or register a custom operator using `JsonLogic().add_operator('my_op', my_function)`.
TypeError: unsupported operand type(s) for +: 'str' and 'int'
An arithmetic or comparison operator in the JsonLogic rule is attempting to operate on incompatible data types (e.g., adding a string to an integer), leading to a Python `TypeError` during evaluation.
fix
Ensure that the data passed to operators has compatible types. Pre-process the input data to convert types as needed before evaluation.
Upgrade
Version history
1.0.1latest on PyPI · released Sep 12, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
panzi-json-logic — pip install panzi-json-logic · libregistry