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 nodeparent()→ Move up the data treecurrent()→ Reference the active nodevar()→ 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jslt-2.2.0.tar.gz.
File metadata
- Download URL: jslt-2.2.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5707028e0e8d1687011c5025c49b5e3c2a405a87e95a4b87e5ebda9d1377040a
|
|
| MD5 |
7c384123fad20664027fa4957f6f5aa0
|
|
| BLAKE2b-256 |
697d94609ab04ea835aa77c07107ef399f96fc3176d7a114dabc644be4bc21f1
|
File details
Details for the file jslt-2.2.0-py3-none-any.whl.
File metadata
- Download URL: jslt-2.2.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d887304be7dd32402fd2f7a0a012b6718d4a8356e183c3cc62b79da803a80f7
|
|
| MD5 |
ab98888bb23bec80eea028ac9d32d26e
|
|
| BLAKE2b-256 |
cf2e0655aff5636f8ebe18ee8cd0c21eb47211078a865fb7792e98d4b2fa9ac6
|