PHPackages                             mehlah/li3\_jbuilder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mehlah/li3\_jbuilder

ActiveLithium-library[Utility &amp; Helpers](/categories/utility)

mehlah/li3\_jbuilder
====================

Create JSON structures via a DSL

18PHP

Since Apr 12Pushed 13y ago3 watchersCompare

[ Source](https://github.com/mehlah/li3_jbuilder)[ Packagist](https://packagist.org/packages/mehlah/li3_jbuilder)[ RSS](/packages/mehlah-li3-jbuilder/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

### Adding JSON Responses to an Application [![Build Status](https://camo.githubusercontent.com/47e9de9dd8b9212c0b722af20dd4b469548c72790f9548187e60367355e32b63/68747470733a2f2f7472617669732d63692e6f72672f6d65686c61682f6c69335f6a6275696c6465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/mehlah/li3_jbuilder)

[](#adding-json-responses-to-an-application-)

To demonstrate the befinits of this libary, we’ll use a simple application. This app has many `discussions` and we’d like to add a JSON representation for each `discussion`that we can fetch by appending `.json` to the discussion’s URL. If we try this we’ll see the JSON representation of the discussion.

[![](https://camo.githubusercontent.com/4df6dd259532264a7d25f833bf34e316ac8fc4bd96168d6e65749b93e9bc36f9/687474703a2f2f63313335323230312e72312e6366332e7261636b63646e2e636f6d2f313334333038393930382e6a7067)](https://camo.githubusercontent.com/4df6dd259532264a7d25f833bf34e316ac8fc4bd96168d6e65749b93e9bc36f9/687474703a2f2f63313335323230312e72312e6366332e7261636b63646e2e636f6d2f313334333038393930382e6a7067)

### Customizing The Response

[](#customizing-the-response)

The JSON returned includes all of the discussion’s attributes but what if we want to customize it? This is where things can start to get ugly. We can call `to('json')` on the discussion and customize what’s returned. Let’s say that we want the id, subject and content fields from the discussion along with its author and the same fields from the discussion’s messages.

```
public function show() {
	$discussion = Discussions::find($this->request->id);

	if (!$discussion) {
		return $this->redirect('/discussions');
	}

	if ($this->request->is('json')) {
		// we don't want to expose this
		unset($discussion->created_at);

		// additional data comes from associated Author and Comments records.
		$discussion->author = [
			'name' => 'Mehdi Lahmam B.',
			'id' => '1234'
			];
		$discussion->comments = [
			['content' => 'w00t', 'author' => 'John Doe'],
			['content' => 'This is sexy', 'author' => 'Jimmy']
		];

		return compact('discussion');
	}

	return compact('discussion');
}
```

We can try this by reloading the page again. When we do we see the customized JSON response including the associated Author and Comments records.

[![](https://camo.githubusercontent.com/7f669ba7b3401262d797c5b7a204b09ce42e037bafd4cb9cdad4003511d85cc1/687474703a2f2f63313335323230312e72312e6366332e7261636b63646e2e636f6d2f313334333039303633332e6a7067)](https://camo.githubusercontent.com/7f669ba7b3401262d797c5b7a204b09ce42e037bafd4cb9cdad4003511d85cc1/687474703a2f2f63313335323230312e72312e6366332e7261636b63646e2e636f6d2f313334333039303633332e6a7067)

This works, but the code we’ve used isn’t very pretty.

### A better way

[](#a-better-way)

Coming soon !

#### Credits

[](#credits)

Loosely based on the [original](https://github.com/rails/jbuilder), and a [PHP port](https://github.com/dhotson/JBuilder-php)

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/67217c861f2a746f3289f095113c13725ee3998fff89355c3d31e3dd3351f527?d=identicon)[mehlah](/maintainers/mehlah)

---

Top Contributors

[![mehlah](https://avatars.githubusercontent.com/u/224928?v=4)](https://github.com/mehlah "mehlah (10 commits)")

### Embed Badge

![Health badge](/badges/mehlah-li3-jbuilder/health.svg)

```
[![Health](https://phpackages.com/badges/mehlah-li3-jbuilder/health.svg)](https://phpackages.com/packages/mehlah-li3-jbuilder)
```

###  Alternatives

[nasution/terbilang

Convert numbers into words (and vice-versa) in Indonesian language

6548.1k](/packages/nasution-terbilang)[bastinald/laravel-livewire-modals

Dynamic Laravel Livewire Bootstrap modals.

6822.0k1](/packages/bastinald-laravel-livewire-modals)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
