Skip to main content

Homogeneous JSON template engine

Project description

transon

PyPI PyPI - Python Version Codecov PyPI - Downloads

Homogeneous JSON template engine. transon creates JSON out of template and JSON input.

                    ┌─────────────────┐
                    │  JSON Template  │
                    └────────┬────────┘
                             │
┌──────────────┐    ┌────────▼────────┐    ┌───────────────┐
│  JSON Input  ├────►     transon     ├────►  JSON Output  │
└──────────────┘    └─────────────────┘    └───────────────┘

Documentation and playground: https://transon-org.github.io/

transon is a powerful and flexible JSON template engine that enables developers to transform JSON data using a customizable set of rules. With transon, you can generate dynamic templates, manipulate JSON data, and produce new JSON structures that meet your specific requirements.

Development Principles

transon was built with a set of key development principles in mind, including:

  • Flexibility and Extensibility: transon is designed to be highly flexible and extensible, allowing you to add new rules and types of placeholders to suit your unique needs.
  • Valid JSON Structure: transon templates are defined as valid JSON structures, making them easy to work with and compatible with a wide range of tools and applications.
  • Composable Rules: transon rules are highly composable, allowing you to define complex behavior patterns using a combination of nested rules. For example, arithmetic expressions can be defined with nested rules, where each rule represents a specific operation. This approach eliminates the need for a domain-specific language (DSL) for arithmetic expressions.
  • Marker-Based Templates: The most important aspect of a transon template is the use of the $ marker. This marker is a special key within the JSON structure that distinguishes it from other types of JSON data. By default, the $ key is used as the marker, but you can change it to any other value you prefer.

By using a marker-based approach, transon ensures that templates are easy to work with and can be easily distinguished from other types of JSON data. This makes it simple to generate dynamic templates, manipulate JSON data, and produce new JSON structures that meet your specific requirements. Additionally, the composable rules approach allows for advanced behavior patterns that can be defined using a combination of nested rules, making transon highly flexible and extensible.

Installation

transon can be installed using pip, the Python package manager. Simply run the following command:

pip install transon

Development

Requires Python 3.9+ and uv.

uv sync --dev
uv run pytest .

Inspired by

Comparison

JSON transformation is a crowded space. transon's bet is that templates are themselves pure JSON — storable, generatable, diff-able, and extensible with your own rules — traded against the terseness of a string DSL. Pick the tool that fits the job:

Tool Template / language Extensible Deps & runtime Best when
transon pure JSON tree custom rules, operators, functions none (Python stdlib) templates must be stored / generated / validated as JSON, with domain-specific rules, in Python
JSONata string expression DSL limited JS library concise queries & expressions over JSON in JavaScript
jq string filter language limited native binary CLI piping and ad-hoc filtering in the shell
JSLT string DSL (jq-like) user functions JVM compact JSON→JSON on the JVM
Jolt JSON spec limited JVM declarative structural reshaping on the JVM
JsonLogic JSON logic tree limited small libs (many languages) portable business/boolean rules shared across services
JSON-e JSON template limited JS / Python parameterising JSON config with interpolation
Jsonnet full templating language yes native binary generating large config (e.g. Kubernetes) from a real language
json-templates JSON with {{placeholders}} no tiny JS library simple value substitution into a JSON skeleton

Where transon is not the best pick (worth being honest about):

  • Expression-heavy transforms read far more concisely in a string DSL like JSONata or jq — transon spells (a + b) * c as a nested rule tree.
  • Maturity & ecosystem: jq and JSONata are battle-tested with large communities; transon is young and Python-only.

The trade-off, concretely

The same transform — multiply each order's qty by its price — over input {"orders": [{"qty": 2, "price": 3}, {"qty": 5, "price": 7}]}:

JSONata — a terse string expression:

orders.(qty * price)

transon — pure JSON, composable rules:

{
  "$": "chain",
  "funcs": [
    {"$": "attr", "name": "orders"},
    {
      "$": "map",
      "item": {
        "$": "expr",
        "op": "mul",
        "values": [
          {"$": "attr", "name": "qty"},
          {"$": "attr", "name": "price"}
        ]
      }
    }
  ]
}

Both yield [6, 35]. JSONata wins on brevity; transon wins when the template itself must be data — stored in a database, generated by another program, reviewed as a diff, checked with Transformer.validate(), or extended with your own rules.

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

transon-0.1.0.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

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

transon-0.1.0-py3-none-any.whl (45.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: transon-0.1.0.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for transon-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c63f3fa0b2f9ae602a94769c5ed3a14f202af1311e558bbcbae0ede5da2f3753
MD5 a2d4e0aa2fb55a1ef77d71cf9203394a
BLAKE2b-256 05e0f955263ad0667151515a80b2c02caf5300f168dd10d0a8b2644b19f0381c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: transon-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 45.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for transon-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e33d8342f221274539b6ff1d3a86691a47d5521f46c153d9557c14cc7ec08f6c
MD5 cae71097717d52a5efc40045a312c3fb
BLAKE2b-256 678e61315530bd97a4953584a5bb9569e40b6e8585120f270de7703399ed55ce

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