Skip to main content

An extensible and sane implementation of JsonLogic

Project description

Supported Python versions PyPI - Version PyPI - Version

python-jsonlogic is an extensible and sane implementation of JsonLogic, making use of the JSON Schema specification.

Motivation

While the JsonLogic format can be great to serialize logic, it lacks a formal specification and some aspects are unclear/unspecified:

  • operators arguments can take any value. For instance, comparison operators are said to work with “numeric” values, however the JavaScript playground doesn’t validate inputs. It is also convenient to allow such comparison operators for date and datetime objects as well.

  • Operators accessing data use a dot-like notation, which is ambiguous when dealing with keys such as my.key.

  • Operators such as map provides their own data scope, making it impossible to access higher-level data inside the operator expression.

For these reasons, python-jsonlogic provides a way to typecheck your JSON Logic expressions at “compile” time, before applying input data to them.

Installation

From PyPI:

pip install python-jsonlogic

The library can be imported from the jsonlogic module.

Usage

# 1. Create or use an already existing operator registry:
from jsonlogic.operators import operator_registry

# 2. Parse the JSON Logic expression:
from jsonlogic import JSONLogicExpression

expr = JSONLogicExpression.from_json({"map": [
    [1, 2],
    {"*": [{"var": ""}, {"var": "/my_int@1"}]},
]})

# 3. Create an operator tree:
root_op = expr.as_operator_tree(operator_registry)

# 4. Typecheck the expression:
from jsonlogic.typechecking import typecheck

typ, diagnostics = typecheck(
    root_op,
    data_schema={
      "type": "object",
      "properties": {
            "my_int": {"type": "integer"}
        },
    }
)
print(typ)
#> ArrayType(IntegerType())

# 5. Evaluate with data:
from jsonlogic.evaluation import evaluate
value = evaluate(
    root_op,
    data={"my_int": 2},
    data_schema=None,
)
print(value)
#> [2, 4]

Project details


Download files

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

Source Distribution

python_jsonlogic-0.1.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

python_jsonlogic-0.1.0-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file python_jsonlogic-0.1.0.tar.gz.

File metadata

  • Download URL: python_jsonlogic-0.1.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for python_jsonlogic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 581faad0e9adaa3c343bb74c02d73ffe23b10b94a0373e7cb4875a50efa13b9f
MD5 86e106f9bc1be26a6a67f6fa05a3b80b
BLAKE2b-256 66aa02347287af15ef0efb10b1246b75e5da891411cb6778d5cd011531568640

See more details on using hashes here.

File details

Details for the file python_jsonlogic-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_jsonlogic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e545062f44613cbc7de623a88e1edaf3b4f0981c1f8615dbd85c80b754dced25
MD5 6c7a305cca6d039e111a700e4e1d6331
BLAKE2b-256 9d14c15cb899495cb1e9744b9caad516a0bfca8a8fc15c6665fd7694b2004057

See more details on using hashes here.

Supported by

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