Skip to main content

JSON Logic for Python with JSONPath multi-value resolution via the vars operator

Project description

json-logic-path

A Python implementation of JsonLogic extended with JSONPath-backed multi-value resolution via the vars operator.

Forked from the original json-logic-py and updated for Python 3.11+.

Operators

var -- scalar dot-path resolution

Resolves a single value from the data context using dot notation. List indices are supported as integer path segments. Returns None (or a supplied default) if the path does not exist.

jsonLogic({"var": "user.name"}, {"user": {"name": "Alice"}})
# "Alice"

jsonLogic({"var": "items.0"}, {"items": ["a", "b"]})
# "a"

jsonLogic({"var": ["missing", "default"]}, {})
# "default"

vars -- JSONPath multi-value resolution

Resolves multiple values from the data context using a JSONPath expression (via jsonpath-ng). Always returns a list.

An empty list indicates no matches. An empty list is falsey in Python, so a failed match behaves correctly in boolean branch conditions without any special-casing.

var and vars are distinct operator keys and do not interact.

data = {"tags": ["a", "b", "c"]}
jsonLogic({"vars": "tags[*]"}, data)
# ["a", "b", "c"]

jsonLogic({"vars": "steps[?type='image-eval'].output.score"}, state)
# [0.9, 0.4]

jsonLogic({"vars": "nothing[*]"}, {})
# []  -- falsey

Branch conditions in workflow definitions

var resolves scalar values for threshold comparisons:

rule = {">=": [{"var": "steps.safety_check.output.scores.unsafe"}, 0.7]}
jsonLogic(rule, workflow_state)
# False -- score is 0.1, do not halt

vars collects values across parallel step outputs for fan-in gates:

rule = {"==": [
    {"count": [{"vars": "steps[?type='image-embed'].output.vector"}]},
    2
]}
jsonLogic(rule, workflow_state)
# True -- both embed steps have produced output

Composite conditions combining both:

rule = {
    "and": [
        {"<":  [{"var": "steps.safety_check.output.scores.unsafe"}, 0.3]},
        {">=": [{"var": "steps.aesthetic.output.scores.aesthetic"}, 0.7]},
    ]
}

List operations

map, filter, and reduce work against lists produced by vars:

# filter scores above threshold
jsonLogic(
    {"filter": [{"vars": "scores[*]"}, {">=": [{"var": ""}, 0.7]}]},
    {"scores": [0.9, 0.2, 0.75, 0.4]}
)
# [0.9, 0.75]

# sum all scores
jsonLogic(
    {"reduce": [
        {"vars": "scores[*]"},
        {"+": [{"var": "accumulator"}, {"var": "current"}]},
        0
    ]},
    {"scores": [0.9, 0.2, 0.75]}
)
# 1.85

Input wiring in workflow definitions

var for direct scalar input references (common case):

input:
  text: {"var": "steps.caption.output.text"}

vars for fan-out input references (all items at a path):

input:
  embeddings: {"vars": "steps[?type='image-embed'].output.vector"}

The executor sees a list at an input field and expands it into one dispatch per item. vars is the declaration; the executor is the implementation of the expansion.

Installation

pip install json-logic-path

For development:

pip install -e ".[dev]"
pytest

Compatibility

  • Python 3.11+
  • jsonpath-ng is required for the vars operator and is installed automatically. All other operators work without it.

Licence

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_logic_path-0.0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file json_logic_path-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for json_logic_path-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 34358d8f991cfec327fdc006caf15aa7d68818703c90088b3fc9ad61760c2494
MD5 69b812241e1985af6318d337b44307a2
BLAKE2b-256 7ba8fbdf363df677ca492b18173fb647afb90be6b1a0ad3d4512885438033749

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page