A Python library to parse JavaScript expressions into JsonLogic-compatible objects
Find a file
Chad Taylor 9d219e4940
Some checks failed
Test / Lint (push) Has been cancelled
Test / Run tests (push) Has been cancelled
Test / Run tests-1 (push) Has been cancelled
Test / Run tests-2 (push) Has been cancelled
Merge pull request #1 from tessellator/fix-test-spelling
test: fix spelling of 'expression' in tests
2026-02-27 23:18:43 -06:00
.github/workflows initial commit 2026-02-21 22:02:00 -06:00
src/js_to_json_logic test: fix spelling of 'expression' in tests 2026-02-27 23:17:52 -06:00
tests initial commit 2026-02-21 22:02:00 -06:00
.gitignore initial commit 2026-02-21 22:02:00 -06:00
.python-version initial commit 2026-02-21 22:02:00 -06:00
CHANGELOG.md initial commit 2026-02-21 22:02:00 -06:00
LICENSE initial commit 2026-02-21 22:02:00 -06:00
pyproject.toml initial commit 2026-02-21 22:02:00 -06:00
README.md initial commit 2026-02-21 22:02:00 -06:00
uv.lock initial commit 2026-02-21 22:02:00 -06:00

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.