JSON-e

4.8.2 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This example illustrates the *concept* of data-structure parameterization that JSON-e provides, using Python's standard `json` module for serialization and deserialization. It manually demonstrates a simple substitution process, as a direct, high-level Python API for 'json-e' templating with an explicit `render` function is not readily available in public documentation.

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))

view raw JSON →