Skip to main content

JSON transformation/templating engine

Project description

JSLT

JSLT is a JSON templating and transformation engine. It enables developers to declaratively map, filter, and reshape JSON data using pure JSON templates, powered by JMESPath for data extraction and a safe, stack-based execution model.


📦 Installation

pip install jslt

🚀 Quick Start

import json
from jslt import JSLT

data = {
    "users": [
        {"name": "Alice", "age": 25, "score": 88.5},
        {"name": "Bob", "age": 17, "score": 92.0}
    ],
    "threshold": 18
}

template = {
    # Extract only adult users
    "adults": {
      "jsl:each": {
        "path": "users",
        "template": {
          "jsl:if": {
            "test": "age >= root().threshold",
            "then": {
              "name": {
                "jsl:path": "name"
              }
            }
          }
        }
      }
    },
    
    # Extract only adult users using jmespath
    "adults_jpath": {
      "jsl:var": {
        "name": "threshold",
        "path": "threshold"
      },
      "jsl:each": {
        "path": "users[?age>var('threshold')]",
        "template": {
          "name": {
            "jsl:path": "name"
          }
        }
      }
    },
    
    # Find names with score > 90
    "top_scorers": {"jsl:path": "users[?score>`90`].name"},
    
    # Calculate average score
    "avg_score": {"jsl:eval": "round(sum(users['*'].score) / count(users), 2)"},
    
    # Calculate average score using jmespath
    "avg_score_jpath": {"jsl:path": "avg(users[*].score)"}
}

jslt = JSLT(template)
result = jslt.transform(data)
print(json.dumps(result, indent=2))

Output:

{
  "adults": [
    {
      "name": "Alice"
    }
  ],
  "adults_jpath": [
    {
      "name": "Alice"
    }
  ],
  "top_scorers": [
    "Bob"
  ],
  "avg_score": 90.25,
  "avg_score_jpath": 90.25
}

🔑 Core Features

  • JMESPath Integration: Leverage full JMESPath syntax for powerful data extraction and filtering.
  • Custom DSL Functions: Transform data using internal functions (jsl:var, jsl:if, jsl:each, jsl:keep, jsl:eval, jsl:path).
  • Stack-Based Iteration: Uses an iterative stack engine to process templates, completely avoiding Python recursion limits.
  • Safe Expression Evaluation: Math and logic expressions are evaluated securely via simple_eval(), preventing arbitrary code execution.

📝 Template DSL & Syntax

Templates are standard JSON objects. Each key defines an output field, and its value contains transformation instructions.

🔹 JSL Functions

All operations are prefixed with jsl: (namespace). Object keys are passed to the function as named parameters. If the value is an array, the values are used as positional parameters. The engine provides the following functions:

Function Description
jsl:var Store intermediate values for reuse
jsl:if Conditional branching ([test, then, other])
jsl:each Iterate over arrays and apply child templates ([path, template])
jsl:keep Instructs the engine to keep this object, even if it resolves to None
jsl:eval Evaluate arithmetic/logic expressions
jsl:path Resolve a JMESPath

🧭 JMESPath custom functions

Custom functions are injected into JMESPath to provide additional functionality:

  • Context Helpers:
    • root() → References the root node
    • parent() → Move up the data tree
    • current() → Reference the active node
    • var() → Gives access to previously defined variables
  • Math:
    • multiply() → Multiply all values
  • Text
    • strip() → Remove whitespace from string

📜 License

This project is licensed under the MIT License. See LICENSE for details.

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

jslt-2.0.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

jslt-2.0.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file jslt-2.0.2.tar.gz.

File metadata

  • Download URL: jslt-2.0.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for jslt-2.0.2.tar.gz
Algorithm Hash digest
SHA256 e7c3d7a930ef9ab657c0baf15ae19f8927c31c0006b7f57f5f4aa8ecea1364e0
MD5 4c143c8423a7c65b8b16bd3639d7738e
BLAKE2b-256 c99c228ad5cb722c47f398b94379ef84e13b439070aa95d5cce08b39e9983eae

See more details on using hashes here.

File details

Details for the file jslt-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: jslt-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for jslt-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25e64c41136d40ae4c3aca9222185b233315c31c11b597469e559dac5f92bcd9
MD5 740652d9c1befcc52b6bc82f97e14b2e
BLAKE2b-256 7efd67c4ec11018342b1ae57e07a36930083edd5e1e2545734a5b2680683398a

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