Skip to content

DJsonDynamic JSON Templating

A lightweight PHP library for creating dynamic JSON with loops, conditionals, and variables

DJson

Quick Example ​

JSON Template:

json
{
  "products": {
    "@djson for products as product": {
      "name": "{​{product.name}​}",
      "price": "{​{product.price}​}",
      "@djson if product.onSale": {
        "discount": {
          "original": "{​{product.price}​}",
          "sale": "{​{product.salePrice}​}"
        }
      }
    }
  }
}

PHP Code:

php
use Qoliber\DJson\DJson;

$djson = new DJson();
$template = file_get_contents('template.json');

$data = [
    'products' => [
        ['name' => 'Laptop', 'price' => 999, 'onSale' => true, 'salePrice' => 799],
        ['name' => 'Mouse', 'price' => 29, 'onSale' => false]
    ]
];

echo $djson->processToJson($template, $data, JSON_PRETTY_PRINT);

Output:

json
{
  "products": [
    {
      "name": "Laptop",
      "price": 999,
      "discount": {
        "original": 999,
        "sale": 799
      }
    },
    {
      "name": "Mouse",
      "price": 29
    }
  ]
}

Installation ​

bash
composer require qoliber/djson

Why DJson? ​

Born from E-commerce Needs

DJson was created to solve a real problem: generating complex JSON structures for e-commerce platforms. When building product catalogs, API responses, and structured data (like Schema.org JSON-LD), we needed a way to create dynamic JSON that was:

  • Easy to read and maintain
  • Type-safe (prices stay numbers, not strings)
  • Reusable across different contexts
  • Simple enough for non-developers to understand

What started as an e-commerce solution has become a powerful library for any JSON generation needs.

Key Benefits:

  • Built for JSON: Unlike general templating engines, DJson is specifically designed for JSON generation
  • Clean Syntax: Directives use a consistent @djson prefix, making templates readable
  • Type Safe: Preserves data types without unwanted conversions - critical for e-commerce pricing
  • Well Tested: 103 tests with 385 assertions, including mutation testing
  • Production Ready: Battle-tested in real-world e-commerce applications

Use Cases ​

  • API Responses: Generate dynamic REST/GraphQL responses
  • Configuration: Create config files based on environment
  • Schema.org: Build JSON-LD structured data
  • E-commerce: Product catalogs, pricing, inventory
  • CMS: Blog posts, comments, dynamic content
  • Reporting: Generate JSON reports from data

Features at a Glance ​

FeatureSyntaxDescription
Variables{​{variable}​}Dynamic value insertion
Loops@djson for items as itemIterate over arrays
Conditionals@djson if conditionShow/hide content
Match/Switch@djson match variablePattern matching
Computed@djson set total = a * bCalculate values
Functions@djson upper {​{name}​}Transform data
Ternary{​{condition ? yes : no}​}Inline conditions
Logicalcondition && otherComplex logic

Next Steps ​

New to DJson? Start with the Getting Started Guide to learn the basics.

Want to see examples? Check out Real-World Examples for common use cases.

Need API details? Browse the API Reference for complete documentation.

Released under the MIT License.