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 = JsonMacroProcessor(merge_strategy=JsonMergeStrategy.DEEP)

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.2.tar.gz (8.0 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.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jsonmacros-0.0.2.tar.gz
  • Upload date:
  • Size: 8.0 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.2.tar.gz
Algorithm Hash digest
SHA256 e7a6898b28d1645c251140dc08b10f6b80abf72e74906854d1ae0178a4c14c12
MD5 31d51f3bf9ab48e7ad09d4c1a9f14e73
BLAKE2b-256 ca546539d8ac6a5164d820c29b162d06562fdc72e0f5db84ca55279d537ffafb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jsonmacros-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 580ec4944c2def0347ea2d094bcad00f9400f06447de28efed837d60a7dbef34
MD5 d1563523d12605c74a72f3744d260648
BLAKE2b-256 81d768d00328772f9a38fe782de4e097d0c7977b01c5795cc3a3dddbbdcbcb7a

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