Skip to main content

js-to-json-logic

This is a Python library that parses a JavaScript expression and converts it into a JsonLogic-compatible object.

This is useful for cases where you want to allow users to input logical expressions in a familiar syntax, but you want to evaluate them using JsonLogic.

This library is inspired by the JavaScript library js-to-json-logic.

Usage

from json_logic import jsonLogic
from js_to_json_logic import transform_js

rule = transform_js("a > 5 && b < 10")
result = jsonLogic(rule, {"a": 6, "b": 8})

print(result)  # True

Supported JavaScript Syntax

Type Example
Literals 42, "hello", true, false, null
Identifiers a, b, myObject.property
Template literals `Hello, ${name}!`
Arrays [1, 2, 3], [1, 2, ...rest]
Unary operations !a, !!b, -x, +x
Conditionals a > 5, b < 10, c >= 15
Arithmetic a + b, a - b, a * b, a / b, a % b
Equality and inequality a == 5, b !== 10, c === 15, d !== 20
Logical expressions a && b, a || b
If statements if (a > 10) { "large" } else if (a > 5) { "medium" } else { "small" }
Ternaries a > 5 ? "yes" : "no"
Call expressions myFunction(a, b)
Regex literals /^abc$/gi
Arrow functions x => x > 5

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for js-to-json-logic 0.1.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 js-to-json-logic 0.1.0
File Size Uploaded
js_to_json_logic-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for js-to-json-logic 0.1.0
File Interpreter ABI Platform
js_to_json_logic-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / js_to_json_logic-0.1.0.tar.gz

Download URL js_to_json_logic-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8c250d6d3ade774850b83d020bd6f35f9afec9cb9b4d9b5f045461d87ad9bdee
BLAKE2b-256 checksum
How to use checksums
31ef69d14f0267605e83947ef76e981bd775f29f0709e9afee0cc3c8897bbe2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / js_to_json_logic-0.1.0-py3-none-any.whl

Download URL js_to_json_logic-0.1.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
673e89efbdc546288b92e6096c4d1ff096e11483e7485fcacaec984287242bc8
BLAKE2b-256 checksum
How to use checksums
8f9d8f93720296f022f36dbd49dbfb4fe9fb111f7681eec027d297d9899577a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.0 This release

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