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

{
    "$macros": [
        {
            "name": "greeting",
            "body": "Hello, world!"
        },
        {
            "name": "number",
            "body": 42
        }
    ],
    "message": "${greeting}",
    "value": "${number}"
}

Parameterized Macros

{
    "$macros": [
        {
            "name": "polyfit",
            "params": ["x", "y", "deg"],
            "body": "${np.polyfit(x, y, deg).tolist()}"
        }
    ],
    "coefficients": {
        "$macro": "polyfit",
        "$params": {
            "x": [0, 1, 2, 3, 4],
            "y": [5, 6, 9, 14, 21],
            "deg": 2
        }
    }
}

Conditionals

{
    "$macros": [
        {
            "name": "score",
            "body": 85
        }
    ],
    "result": {
        "$if": "${score >= 90}",
        "$then": "Excellent",
        "$else": {
            "$if": "${score >= 70}",
            "$then": "Good",
            "$else": "Needs Improvement"
        }
    }
}

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
    }
}

API Reference

JsonMacroProcessor

The main class for processing JSON with macros.

from jsonmacros import JsonMacroProcessor, JsonMergeStrategy

processor = jsonmacros.JsonMacroProcessor(merge_strategy=jsonmacros.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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

jsonmacros-0.0.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonmacros-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a643aad3e688337a6431840eeb7716cab303dd2d88bfc46f54562b71a5273e5a
MD5 6ee4017bb21da05861104fe393976654
BLAKE2b-256 ab329856172ba468078367220e2ee95851e232290cad982726bc8bb10ca908a2

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