Skip to main content

A utility for validating and rendering templates with placeholders in JSON-like strings.

Project description

JSON Interpolator

Overview

The JSON Interpolator is a Python package for validating and rendering templates with placeholders in JSON-like strings. It ensures placeholders are correctly formatted and substitutes them with provided values.

Features

  • Validation: Ensures placeholders meet naming conventions.
  • Rendering: Dynamically replaces placeholders with values from a dictionary.
  • Object-Oriented Design: Encapsulated within the JSONInterpolator class for easy reuse and extension.

Placeholder Format

Placeholders should follow the syntax:

<<placeholder_name>>

Where:

  • placeholder_name cannot start with a digit.
  • placeholder_name must include at least one word character (letters, digits, or underscores).

Installation

Install from PyPI:

pip install json-interpolator

Or clone the repository for local development:

git clone https://github.com/mgeborges/json-interpolator.git
cd json-interpolator
pip install -e .

Usage

Example

from json_interpolator import JSONInterpolator

template = """
    {
        "name": <<name>>,
        "age": <<age>>,
        "roles": <<roles>>,
        "contact_info": <<contact_info>>,
        "is_active": <<is_active>>
    }
"""
params = {
    "name": "Alice",
    "age": 30,
    "roles": ["admin", "support"],
    "contact_info": {"email": "alice@company.com", "phone": "(555) 555-5555"},
    "is_active": True
}

result = JSONInterpolator.render_template(params, template)
print(result)
# Outputs:
# {
#     "name": "Alice",
#     "age": 30,
#     "roles": ["admin", "support"],
#     "contact_info": {"email": "alice@company.com", "phone": "(555) 555-5555"},
#     "is_active": true
# }

Please note that there is no need to wrap string placeholders with quotes inside templates. Doing so will lead to an invalid JSON.

Error Handling

Raises:

  • ValueError if
    • invalid placeholders are found. Placeholder names are expected to follow the Python variables naming standards.
    • an invalid JSON is generated because of bad template formatting.
  • TypeError if the template is not a string or if the parameters are not provided in a dictionary.

Testing

The package includes a test suite in the tests/ directory. To run tests:

Using unittest

Run tests from the root directory:

python -m unittest discover -s tests

Using pytest

If you prefer pytest:

pip install pytest
pytest

Dependencies

This project does not require external dependencies beyond the standard library.

License

MIT License

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

json_interpolator-1.0.0rc1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

json_interpolator-1.0.0rc1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file json_interpolator-1.0.0rc1.tar.gz.

File metadata

  • Download URL: json_interpolator-1.0.0rc1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for json_interpolator-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 5679ad5d7e042a7ac1e7d8450fb0a384f22d0e5fbba1ab949f451ca8c29f7631
MD5 cab356085a37921b82c0cf4731bc3021
BLAKE2b-256 3c3d8be672ddd4ecf67635dd62ad2d679eda9c3ecad50b489b0822abe3732d99

See more details on using hashes here.

File details

Details for the file json_interpolator-1.0.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for json_interpolator-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 502a854c199769b6c3b2f48d39352d06345a286673c4f952f9f075387758836f
MD5 11c13285fba2f30c7ad85c3771ab8c01
BLAKE2b-256 0f4053ac1e9aa36a3061f5de1a697f9bba454e5fa5f050fed0b8ccd317d3b57b

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