PHPackages                             saad/json-response-builder - 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. [API Development](/categories/api)
4. /
5. saad/json-response-builder

ActiveLibrary[API Development](/categories/api)

saad/json-response-builder
==========================

this package semplifies sending API json response by using a standard and unique response structure

v1.6(4y ago)59.2k11MITPHPPHP &gt;=7.2CI failing

Since Feb 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ahmad-sa3d/json-response-builder)[ Packagist](https://packagist.org/packages/saad/json-response-builder)[ RSS](/packages/saad-json-response-builder/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (10)Used By (1)

 [![](https://camo.githubusercontent.com/640c3d52b2764f179ef3cf089b604516a8c4ac0a06f055a46c6a7fce9428b787/68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d6c61726176656c2e737667)](https://camo.githubusercontent.com/640c3d52b2764f179ef3cf089b604516a8c4ac0a06f055a46c6a7fce9428b787/68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d6c61726176656c2e737667)

Json Response Builder Package
=============================

[](#json-response-builder-package)

[![Build Status](https://camo.githubusercontent.com/5e72e1da89b9504eda02fb83c63f44ab2cac7d51d898291f54f5fdc72adad565/68747470733a2f2f7472617669732d63692e6f72672f61686d61642d736133642f6a736f6e2d726573706f6e73652d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ahmad-sa3d/json-response-builder)[![](https://camo.githubusercontent.com/b39afed68fcadeb0044d2d850f986b29b2d419982850a8dd54f2190426f28d56/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38346437303938313461333230646338356630612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/ahmad-sa3d/json-response-builder/maintainability)[![](https://camo.githubusercontent.com/c8e424e8c0ae04d61ee0c83044a081c7bf88d1c693942fa2da2424cca13314e6/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38346437303938313461333230646338356630612f746573745f636f766572616765)](https://codeclimate.com/github/ahmad-sa3d/json-response-builder/test_coverage)[![License](https://camo.githubusercontent.com/f45d904953153ca304a2328243d2733e095eee13a631a1f390709885d41dd692/68747470733a2f2f706f7365722e707567782e6f72672f6c61726176656c2f6672616d65776f726b2f6c6963656e73652e737667)](https://packagist.org/packages/saad/json-response-builder)

### Install

[](#install)

```
	composer require saad/json-response-builder
```

### Change Log

[](#change-log)

> `V 1.3.1`

add feature to merge meta when adding data

`addData(str $key, mix $val, bool $merge_meta)`

```

	$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();

	$builder->addData('users', [
		['name' => 'Ahmed Saad'],
		'meta' => [
			'name' => 'iam meta',
		]
	], true); // Note the third argument

	return $builder->getResponse();

	// Output

	{
        "success": true,

        "meta" : {
            'name' => 'iam meta',
        },

        "data": [
            'users': [
            		{"name": "Ahmed Saad"}
            ],
        ],

        "message": "Successfully Retrieved"
    }
```

> `V 1.3`

Add Strict Mode and enabled by default: in strict mode if data or meta are empty it will set it's value to `null` instead of `[]`

to turn of strickt mode, on constructor pass `false` to disable strickt mode so that it will return data, and meta as empty array if they are empty

you can set mode on instance by the method `strictMode(bool)`

### Usage

[](#usage)

- Basic Example:

    inside your controller:

    ```

     	$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();

     	$builder->mergeData([
     		['name' => 'Ahmed Saad'],
     		['name' => 'John Doe']
     	]);

     	$builder->addMeta([
     		'pagination' => [
     			'page' => 1,
     			'per_page' => 4,
     		]
     	]);

     	return $builder->getResponse();

    ```
- the above example will output:

    ```

     	{
     		"success": true,

     		"meta" : {
     			"pagination": {
     				"page": 1,
     				"per_page": 4
     			}
     		},

     		"data": [
     			{"name": "Ahmed Saad"},
     			{"name": "John Doe"}
     		],

     		"message": "Successfully Retrieved"
     	}
    ```

Available Methods
-----------------

[](#available-methods)

### `addData($key, $value)`

[](#adddatakey-value)

> Appends to data new member with the given key and value
>
> ```
> 	$builder->addData('doctors', ['ahmed', 'mohamed', 'saad']);
>   $builder->addData('patients', ['patient1', 'patient3', 'patient3']);
>
> 	// Output data will be
>
>   "data": {
>   	"doctors": ["ahmed", "mohamed", "saad"],
>   	"patients" ["patient1", "patient4", "patient3"]
>   },
> ```

### `mergeData($array)`

[](#mergedataarray)

> merge given array with data with given array keys as keys, this is usefull when we want to send data as json array insteadof json object with key and value
>
> this method also if the given array has key called 'meta' it will remove that key and add it to response meta
>
> ```
> 	$builder->mergeData(['ahmed', 'mohamed', 'meta' => ['key' => 'Iam Meta']]);
>
> 	// Output will be
>
> 	{
>    	"success": true,
>    	"meta": {
>    		"key": "Iam Meta"
>    	},
>   	"data": [
>   		"ahmed",
>   		"mohamed"
>   	],
>   	"message": "Successfully Retrieved"
>   }
>
> ```

### `addMeta($key, $value)`

[](#addmetakey-value)

> Appends to meta new member with the given key and value

### `mergeMeta($array)`

[](#mergemetaarray)

> merge given array with meta

### `addHeader($header, $value)`

[](#addheaderheader-value)

> add header to response headers

### `success($response_message = null)`

[](#successresponse_message--null)

> set response success status to **`true`**, and set response message if supplied.

### `setMessage($response_message = null)`

[](#setmessageresponse_message--null)

> set response message if supplied.

### `error($message = null, $error_code = null)`

[](#errormessage--null-error_code--null)

> set response success status to **`false`** and set nessage and error code
>
> ```
>  $builder->error('Fails!', 2345);
>
>  // Output will be
>
>  {
>   	"success": false,
>   	"meta": null,
>  	"data": null,
>  	'error': {
>  		"message": "Fails!",
>  		"code": 2345
>  	}
>  	"message": "Fails!"
>  }
> ```

### `addError($key, mixed $value)`

[](#adderrorkey-mixed-value)

> Add key to error array
>
> ```
>  $builder->error('Fails!', 2345)
>  	->addError('validation', 'validation value');
>
>  // Output will be
>
>  {
>   	"success": false,
>   	"meta": null,
>  	"data": null,
>  	'error': {
>  		"message": "Fails!",
>  		"code": 2345,
>  		"validation": "validation value"
>  	}
>  	"message": "Fails!"
>  }
> ```

### `setStatusCode(301)`

[](#setstatuscode301)

> set response status code.

### `strictMode(bool)`

[](#strictmodebool)

> default value `true`
>
> since `V1.3`
>
> enable or disable strict mode.
>
> ```
>  $builder->getResponse();
>
>  // Strict Mode Enabled, Output will be
>
>  {
>   	"success": false,
>   	"meta": null,
>  	"data": null,
>  	"message": ""
>  }
>
>  $builder->strictMode(false)->getResponse();
>
>  // Strict Mode Disabled, Output will be
>
>  {
>   	"success": false,
>   	"meta":[],
>  	"data":[],
>  	"message": ""
>  }
> ```

### `getResponse($status_code = null)`

[](#getresponsestatus_code--null)

> set response status code if supplied, and return Response Object

License
-------

[](#license)

The Laravel framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).README.md

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~151 days

Recently: every ~292 days

Total

9

Last Release

1802d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.6.4

1.4PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/7927d2c9296987c6329eeda722cee82eb4622cff56b2b628f6a698a05e9ddad1?d=identicon)[ahmad-sa3d](/maintainers/ahmad-sa3d)

---

Top Contributors

[![ahmad-sa3d](https://avatars.githubusercontent.com/u/8466669?v=4)](https://github.com/ahmad-sa3d "ahmad-sa3d (19 commits)")

---

Tags

jsonjson-apilaravellaravel-5-packageresponserestful-api

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/saad-json-response-builder/health.svg)

```
[![Health](https://phpackages.com/badges/saad-json-response-builder/health.svg)](https://phpackages.com/packages/saad-json-response-builder)
```

###  Alternatives

[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[resend/resend-laravel

Resend for Laravel

1191.4M6](/packages/resend-resend-laravel)[flat3/lodata

OData v4.01 Producer for Laravel

96320.9k](/packages/flat3-lodata)[joggapp/laravel-aws-sns

Laravel package for the SNS events by AWS

3171.8k](/packages/joggapp-laravel-aws-sns)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[spatie/spatie-price-api

The Price API used at promotional sites for our own products

1515.1k1](/packages/spatie-spatie-price-api)

PHPackages © 2026

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