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.
- Validation: Ensures placeholders meet naming conventions.
- Rendering: Dynamically replaces placeholders with values from a dictionary.
- Object-Oriented Design: Encapsulated within the
JSONInterpolatorclass for easy reuse and extension.
Placeholders should follow the syntax:
<<placeholder_name>>
Where:
placeholder_namecannot start with a digit.placeholder_namemust include at least one word character (letters, digits, or underscores).
Install from PyPI:
pip install json-interpolatorOr clone the repository for local development:
git clone https://github.com/mgeborges/json-interpolator.git
cd json-interpolator
pip install -e .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.
Raises:
ValueErrorif- 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.
TypeErrorif the template is not a string or if the parameters are not provided in a dictionary.
The package includes a test suite in the tests/ directory. To run tests:
Run tests from the root directory:
python -m unittest discover -s testsIf you prefer pytest:
pip install pytest
pytestThis project does not require external dependencies beyond the standard library.
MIT License