Skip to main content

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, $else for 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:

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

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

jsonmacros-0.0.4.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

jsonmacros-0.0.4-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonmacros-0.0.4.tar.gz.

File metadata

  • Download URL: jsonmacros-0.0.4.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

Hashes for jsonmacros-0.0.4.tar.gz
Algorithm Hash digest
SHA256 4cddc7ad6916b3ccfaae3270fac7c225caf04e226f12bb4daaf87ef63e5c02f0
MD5 7869adc21515bdcbb18f95a8db2aa3ad
BLAKE2b-256 67a6e8a827ca1fdd729677d5ab244e7d19f1e47b02060d5f253aad5b80762845

See more details on using hashes here.

File details

Details for the file jsonmacros-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: jsonmacros-0.0.4-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

Hashes for jsonmacros-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a239b64d63b67931df80425f37c6498aebc26fbd2cc56a1486a5a4652d40ba7b
MD5 1d7a19de51cff2225fba2ed07f195c50
BLAKE2b-256 30dd9381bd4d5b69d7bd7840caff1c5bb649fa02181436090a2e5096cf1e749e

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