A JSON preprocessor with macros, conditionals, and includes
Project description
JSONMacros
A JSON preprocessor with macros, conditionals, and includes for Python.
Overview
JSONMacros allows you to write dynamic JSON files with macro definitions, conditional logic, file includes, and Python expression evaluation. It's perfect for configuration files, data templates, and any scenario where you need programmatic JSON generation.
Features
- Macros: Define reusable variables and functions with parameters
- Conditionals: Use
$if,$then,$elsefor conditional content - File Includes: Import and merge other JSON files with
$include - Python Expressions: Evaluate Python code within JSON using
${...}syntax - Flexible Merging: Control how included files are merged
Installation
pip install jsonmacros
Quick Start
from jsonmacros import JsonMacroProcessor
processor = JsonMacroProcessor()
result = processor.process_file("path/to/your/template.json")
print(result)
Usage Examples
Basic Macros
Input
{
"$macros": [
{
"name": "greeting",
"body": "Hello, world!"
},
{
"name": "number",
"body": 42
}
],
"message": "${greeting}",
"value": "${number}"
}
Output
{
"message": "Hello, world!",
"value": 42
}
Parameterized Macros
Input
{
"$macros": [
{
"name": "add",
"params": ["x", "y"],
"body": "${x + y}"
}
],
"value": {
"$macro": "add",
"$params": {
"x": 6,
"y": 7
}
}
}
Output
{
"value": 13
}
Conditionals
Input
{
"$macros": [
{
"name": "score",
"body": 85
}
],
"result": {
"$if": "${score >= 90}",
"$then": "Excellent",
"$else": {
"$if": "${score >= 70}",
"$then": "Good",
"$else": "Needs Improvement"
}
}
}
Output
{
"result": "Good"
}
File Includes
Create modular JSON files by including others:
base.json:
{
"$macros": [
{
"name": "app_name",
"body": "MyApp"
}
],
"config": {
"name": "${app_name}",
"version": "1.0.0"
}
}
extended.json:
{
"$include": ["base.json"],
"$macros": [
{
"name": "environment",
"body": "production"
}
],
"config": {
"environment": "${environment}",
"debug": false
}
}
output.json (with merge strategy MERGE):
{
"config": {
"name": "MyApp",
"version": "1.0.0",
"environment": "production",
"debug": false
}
}
API Reference
JsonMacroProcessor
The main class for processing JSON with macros.
from jsonmacros import JsonMacroProcessor, JsonMergeStrategy
processor = JsonMacroProcessor(merge_strategy=JsonMergeStrategy.OVERRIDE)
JsonMergeStrategy
Controls how included files are merged:
- Define merge strategies for handling conflicts and combining data
Examples
See the examples/ directory for more comprehensive examples:
- examples/test.json - Parameterized macros with Python expressions
- examples/merge_a.json - Base configuration with macros
- examples/merge_b.json - Extended configuration with includes and conditionals
Requirements
- Python >=3.9
License
This project is licensed under the BSD 3-Clause License - see the LICENSE.md file for details.
Contributing
Issues and contributions are welcome! Please visit the GitHub repository to report bugs or request features.
Authors
- Broderick Riopelle broderio@umich.edu
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonmacros-0.0.5.tar.gz.
File metadata
- Download URL: jsonmacros-0.0.5.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d930d1a8ee56de482e957662946b4eed3a363a2365e6499b735bf0735dc33cff
|
|
| MD5 |
aa2951ea1d217397f7780ae4c6bf8894
|
|
| BLAKE2b-256 |
eda1e23e5b5790c6c186f87df4193b0baef6d47c9b0194eea761b5e82c01c5b2
|
File details
Details for the file jsonmacros-0.0.5-py3-none-any.whl.
File metadata
- Download URL: jsonmacros-0.0.5-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e044b466d9173827f337f6023bbdd21bcd9b0f4ec9a5933e6f1fd00c9c662873
|
|
| MD5 |
3280f53987b75d9de0f5f0c2371f6771
|
|
| BLAKE2b-256 |
c7a8c14cfbf6287fef2a7f0915ae2234d54b1bae076d86ade22021db1a50569e
|