1 min readMar 29, 2026by jakub

DJson

DJson is a lightweight PHP library for creating dynamic JSON structures using templating directives. It was born out of a real problem: generating complex, nested JSON for e-commerce platforms — things like Schema.org structured data, API responses, and product feeds — where the JSON structure depends on runtime data.

Instead of building JSON arrays in PHP and hoping json_encode produces the right output, DJson lets you write JSON templates with loops, conditionals, and variables that compile down to valid, type-safe JSON.

Website: djson.dev

Installation

Bash
composer require qoliber/djson

Requires: PHP 8.1+ — zero external dependencies.

Key Features

  • Templating directives@djson for loops, @djson if/@djson elseif/@djson else conditionals, @djson match pattern matching, @djson set computed values
  • 25+ built-in functions — String manipulation (upper, lower, trim, slug), number formatting (format_number, round), date handling (date, now), array operations (count, first, last, join)
  • Type safety — Numbers stay numbers, booleans stay booleans, nulls stay nulls. No unwanted string conversions that break JSON consumers
  • Extensible — Register your own custom directives and functions for domain-specific logic
  • Battle-tested — 103 tests, 385 assertions, used in production e-commerce applications

Use Cases

  • Schema.org JSON-LD — Generate structured data for SEO (Product, Organization, BreadcrumbList)
  • API responses — Dynamic response shaping based on request parameters
  • Product feeds — Google Shopping, Facebook Catalog, custom marketplace feeds
  • Configuration files — Environment-aware config generation
  • CMS content — Serialize complex content structures for headless frontends

License

MIT License

Was this page helpful?
DJson — PHP Libraries & Tools — Open Source | qoliber Docs