Skip to main content

Parse JavaScript expressions into JsonLogic-compatible objects.

Project description

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.

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

js_to_json_logic-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

js_to_json_logic-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: js_to_json_logic-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for js_to_json_logic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c250d6d3ade774850b83d020bd6f35f9afec9cb9b4d9b5f045461d87ad9bdee
MD5 7d2b1c44666272905ed186eafb275428
BLAKE2b-256 31ef69d14f0267605e83947ef76e981bd775f29f0709e9afee0cc3c8897bbe2c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: js_to_json_logic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for js_to_json_logic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 673e89efbdc546288b92e6096c4d1ff096e11483e7485fcacaec984287242bc8
MD5 e460c40125c4e0a52f0b0006df690b0a
BLAKE2b-256 8f9d8f93720296f022f36dbd49dbfb4fe9fb111f7681eec027d297d9899577a4

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