Skip to main content

json-operations

Specify complex conditional operations in JSON. The conditional operations can be run securely against a JSON value (Python dictionary) and will return a boolean.

from json_operations import execute
operations = [">", ["key", "items"], 30]
data1 = {
    "items": 31
}
data2 = {
    "items": 29
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

Security

All operations are safe (no use of eval). It's good to enforce a length limit if you are taking input from an untrusted source.

API

execute

Run the json operations against a data dictionary. Return True/False. Can raise a JsonOperationError if operations are invalid or keys don't exist

from json_operations import execute

execute(<operations>, <data_dictionary>) -> bool

get_json_schema

Returns the JSON Schema for json operations. This is useful for validating operations before running them

from json_operations import get_json_schema

get_json_schema() -> Dict

get_keys

Returns the keys inside json operations. This is useful for getting a list of keys necessary for the json operations and validating that all keys are inside the data dictionary

from json_operations import get_keys

get_keys(<operations>) -> List[Dict] 
# [{"name": "key1", "type": "number", "index": 0}, ...]

Operators

== (Equal operator)

Check whether one value equal to another.

Syntax
["==", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["==", ["key", "items"], 30]
data1 = {
    "items": 30
}
data2 = {
    "items": 31
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

!= (Not equal operator)

Check whether one value is not equal to another.

Syntax
["!=", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["!=", ["key", "items"], 30]
data1 = {
    "items": 30
}
data2 = {
    "items": 31
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

> (Greater than operator)

Check whether one value is greater than another.

Syntax
[">", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = [">", ["key", "items"], 30]
data1 = {
    "items": 31
}
data2 = {
    "items": 29
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

>= (Greater than or equal operator)

Check whether one value is greater than or equal to another.

Syntax
[">=", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = [">=", ["key", "items"], 30]
data1 = {
    "items": 30
}
data2 = {
    "items": 29
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

< (Less than operator)

Check whether one value is less than another.

Syntax
["<", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["<", ["key", "items"], 30]
data1 = {
    "items": 31
}
data2 = {
    "items": 29
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

< (Less than or equal operator)

Check whether one value is less or equal to than another.

Syntax
["<=", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["<=", ["key", "items"], 30]
data1 = {
    "items": 31
}
data2 = {
    "items": 30
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

in (In operator)

Check whether one value is contained in another.

Syntax
["in", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["in", "my_type",  ["key", "types"]]
data1 = {
    "types": [
        "type1", "type2"
    ]
}
data2 = {
    "types": [
        "my_type", "type1"
    ]
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

!in (Not In operator)

Check whether one value is NOT contained in another.

Syntax
["!in", <operator_or_literal>, <operator_or_literal>]
Example
from json_operations import execute
operations = ["!in", "my_type",  ["key", "types"]]
data1 = {
    "types": [
        "type1", "type2"
    ]
}
data2 = {
    "types": [
        "my_type", "type1"
    ]
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

btw (Between operator)

Check whether one value is between 2 other values. Equivalent to

low <= val <= high

Both low and high value are included.

Syntax
["btw", <value_operator_or_literal>, [<low_operator_or_literal>, <high_operator_or_literal>]]
Example
from json_operations import execute
operations = ["btw", ["key", "val"],  [1, 3]]
data1 = {
    "val": 2
}
data2 = {
    "val" : 4
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

& (Intersection operator)

Check whether 2 arrays have any members in common

Syntax
["&", <value_operator_or_literal>, <value_operator_or_literal>]
Example
from json_operations import execute
operations = ["&", ["key", "val"],  [1, 2]]
data1 = {
    "val": [2,3]
}
data2 = {
    "val" : [3,4]
}

execute(operations, data1) # -> True
execute(operations, data2) # -> False

!& (Not Intersection operator)

Check whether 2 arrays have no members in common

Syntax
["!&", <value_operator_or_literal>, <value_operator_or_literal>]
Example
from json_operations import execute
operations = ["!&", ["key", "val"],  [1, 2]]
data1 = {
    "val": [2,3]
}
data2 = {
    "val" : [3,4]
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

and (And operator)

Check whether all values are True. and supports nesting other operation inside it (See complex example).

Syntax
["and", ...<operators_or_literals>]
Examples
from json_operations import execute
operations = ["and", ["key", "a"],  ["key", "b"]]
data1 = {
    "a": True,
    "b": False,
}
data2 = {
    "a": True,
    "b": True,
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

or (Or operator)

Check whether any values are True. or supports nesting other operation inside it (See complex example).

Syntax
["or", ...<operators_or_literals>]
Examples
from json_operations import execute
operations = ["or", ["key", "a"],  ["key", "b"]]
data1 = {
    "a": False,
    "b": False,
}
data2 = {
    "a": False,
    "b": True,
}

execute(operations, data1) # -> False
execute(operations, data2) # -> True

Complex Operator Example

from json_operations import execute

# (a > 1 or b < 5) and (c == 0 or d != 10) and (val in e or f >= 0)
operations = ["and", 
                ["or", [">", ["key", "a"], 1], ["<", ["key", "b"], 5]], 
                ["or", ["==", ["key", "c"], 0], ["!=", ["key", "d"], 10]],
                ["or", ["in", "val",  ["key", "e"]], [">=", ["key", "f"], 0]],
              ]
data1 = {
    "a": 0,
    "b": 5,
    "c": 0,
    "d": 9,
    "e": ["val", "another_val"],
    "f": 0,
}
data2 = {
    "a": 0,
    "b": 4,
    "c": 0,
    "d": 9,
    "e": ["val", "another_val"],
    "f": -1,
}
# (0 > 1 or 5 < 5) and (0 == 0 or 9 != 10) and (val in ["val", "another_val"] or 0 >= 0)
execute(operations, data1) # -> False

# (0 > 1 or 4 < 5) and (0 == 0 or 9 != 10) and (val in ["val", "another_val"] or -1 >= 0)
execute(operations, data2) # -> True

Differences from json-logic

https://jsonlogic.com/

  • Operations are always type safe (cannot compare different types). json-logic will automatically convert different types and compare them, which can lead to issues that are tough to find
  • Invalid operations are errors instead of ignored. json-logic tries to avoid all errors and instead continues with unexpected input. This can lead to issues
  • More compact and intuitive syntax:
["or", ["key", "a"], ["key", "b"]]

vs

{"or": [{"var": ["a"]}, {"var": ["b"]}]}
  • Only supports boolean logic. json-logic supports all types of operations (addition, subtraction, etc.), not just boolean logic

Metadata

Release files for json-operations 2.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-operations 2.6.0
File Size Uploaded
json-operations-2.6.0.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-operations 2.6.0
File Interpreter ABI Platform
json_operations-2.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / json-operations-2.6.0.tar.gz

Download URL json-operations-2.6.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6878923d213591437dae83707c464c9933d96ddab0185b2b192113d4b784f983
BLAKE2b-256 checksum
How to use checksums
c96a3a4def95c34c748ea1b5293c9c20cd7d96b2d8ea98f543aad80095e19982
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

Release files / json_operations-2.6.0-py3-none-any.whl

Download URL json_operations-2.6.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
993c277154b0f1924c5857d71fbeddc2eb6e92be0eb8bd8796b2a9fd220ad4f4
BLAKE2b-256 checksum
How to use checksums
b84e0ee6397db05313a14b67886f1e2509b5768598afe9580258709c7d078acc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

Release history Release notifications | RSS feed

This release

2.6.0 This release

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page