JSON-e
JSON-e is a data-structure parameterization system designed to embed context within JSON objects. It allows treating a data structure as a 'template' and transforming it using another data structure as context to produce an output data structure. Unlike string-based templating, JSON-e operates directly on data structures, ensuring valid JSON output. The project maintains a healthy release cadence, with version 4.8.2 being the latest, and aims for stability with breaking changes typically resulting from bug fixes rather than new features.
Warnings
- gotcha The `json-e` PyPI package exists, but its official usage documentation (json-e.js.org) is primarily JavaScript-centric. Direct, high-level Python API usage examples and import paths for explicit JSON-e templating functions are not easily found in public documentation. Users may find themselves defaulting to Python's built-in `json` module for general JSON handling, which does not provide JSON-e's templating capabilities.
- gotcha When working with JSON data in Python, it's a common mistake to confuse `json-e` (a templating system) with Python's built-in `json` module. The `json` module handles serialization and deserialization but does not offer the advanced data-structure parameterization features like `$eval`, `$if`, or `$map` that are central to JSON-e's specification.
- breaking The `json-e` project follows semantic versioning, and breaking changes to the language specification or its implementations (including Python) are possible, though stated to be uncommon and typically result from bug fixes. These changes could arise if a bug fix introduces backward incompatibility, necessitating a major version bump.
Install
-
pip install json-e
Imports
- json_e
import json_e
Quickstart
import json
# NOTE: This quickstart demonstrates the *concept* of JSON-e templating
# using Python's built-in 'json' module for data handling.
# A high-level, standalone Python API for the 'json-e' templating logic
# is not extensively documented in public search results.
# This example *simulates* a basic substitution operation.
template = {
"message": "Hello, ${user.name}! Your ID is ${user.id}.",
"settings": {
"theme": "${config.theme}",
"darkMode": "${config.darkMode}"
}
}
context = {
"user": {
"name": "Alice",
"id": "12345"
},
"config": {
"theme": "dark",
"darkMode": True
}
}
def apply_template(template_data, context_data):
# This is a simplified, manual substitution for demonstration.
# A true JSON-e implementation would handle more complex operations ($eval, $if, $map, etc.)
template_str = json.dumps(template_data)
# Basic string interpolation for demonstration
for key_outer, val_outer in context_data.items():
if isinstance(val_outer, dict):
for key_inner, val_inner in val_outer.items():
placeholder = f"${{{key_outer}.{key_inner}}}"
template_str = template_str.replace(placeholder, str(val_inner))
else:
placeholder = f"${{{key_outer}}}"
template_str = template_str.replace(placeholder, str(val_outer))
# Attempt to parse the result back to a JSON object
return json.loads(template_str)
output = apply_template(template, context)
print(json.dumps(output, indent=2))