PHPackages                             alirezajavadi/jsonize - 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. alirezajavadi/jsonize

ActiveLibrary

alirezajavadi/jsonize
=====================

JSONize: Standardize JSON responses effortlessly! Ensure consistency in your applications with our streamlined library. Simplify data formatting, enhance readability, and save time. JSONize empowers developers to generate clean, organized JSON outputs, perfect for APIs, web, and mobile apps. Revolutionize your workflow today!

1.8.1(12mo ago)41725MITPHPCI passing

Since May 14Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/alirezajavadigit/JSONize)[ Packagist](https://packagist.org/packages/alirezajavadi/jsonize)[ RSS](/packages/alirezajavadi-jsonize/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (1)Versions (12)Used By (0)

JSONize
=======

[](#jsonize)

[![Tests](https://github.com/alirezajavadigit/JSONize/actions/workflows/tests.yml/badge.svg)](https://github.com/alirezajavadigit/JSONize/actions/workflows/tests.yml)[![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/b0b6d5f16e83ecdb2339571189bb6e44a49e79db7b8e5bddf8a9530c7442eb54/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d3838393242462e737667)](https://php.net)

A fluent PHP library for building standardized JSON API responses.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Installation
------------

[](#installation)

```
composer require alirezajavadi/jsonize
```

Quick Start
-----------

[](#quick-start)

JSONize provides two usage modes: **Easy** (static, zero-boilerplate) and **Efficient** (singleton, memory-safe for long-running processes).

### Easy Mode

[](#easy-mode)

```
use JSONize\App\Easy\Response;

Response::message("User created")->status(201)->data(["id" => 42]);
```

Output:

```
{
    "success": true,
    "message": "User created",
    "data": {"id": 42},
    "status": [201, "Created"]
}
```

In Easy mode, the response is automatically sent when the object is destroyed. Call `->get()` instead if you need the JSON string returned without echoing:

```
$json = Response::message("OK")->status(200)->get();
```

### Efficient Mode

[](#efficient-mode)

```
use JSONize\App\Efficient\Response;

$response = Response::getInstance();

$response->message("User created")->status(201)->data(["id" => 42])->get();
```

Efficient mode uses a singleton so you reuse the same instance. Always call `->get()` to retrieve the JSON string.

API Reference
-------------

[](#api-reference)

All methods are chainable and available in both modes.

### `message(string $message)`

[](#messagestring-message)

Sets the response message.

```
Response::message("Operation successful");
```

### `status(int $code, string $message = "")`

[](#statusint-code-string-message--)

Sets the HTTP status code. Optionally pass a custom status message to override the default.

```
Response::status(404);
Response::status(200, "Everything is fine");
```

### `data(mixed $data, string $key = "data")`

[](#datamixed-data-string-key--data)

Sets the response payload. The second argument changes the JSON key name.

```
Response::data(["id" => 1, "name" => "Item"]);
Response::data(["id" => 1, "name" => "Item"], "item");
```

With a custom key, the output uses that key instead of `"data"`:

```
{
    "success": true,
    "item": {"id": 1, "name": "Item"},
    "status": [200, "OK"]
}
```

### `error(string $message, int $status = 500)`

[](#errorstring-message-int-status--500)

Shorthand for setting an error message and status code in one call.

```
Response::error("Something went wrong");
Response::error("Validation failed", 422);
```

### `headers(array $headers)`

[](#headersarray-headers)

Merges custom HTTP headers into the response. CORS headers are included by default.

```
Response::message("OK")->headers(["X-Request-Id" => "abc-123"]);
```

### `hideStatus()` / `hideMessage()` / `hideData()` / `hideSuccess()`

[](#hidestatus--hidemessage--hidedata--hidesuccess)

Remove specific fields from the JSON output.

```
Response::message("OK")->status(200)->hideStatus();
```

```
{
    "success": true,
    "message": "OK",
    "data": null
}
```

### `get()`

[](#get)

Returns the JSON string and prevents automatic output (Easy mode). In Efficient mode, this is the only way to get the response.

```
$json = Response::message("OK")->get();
```

Full Examples
-------------

[](#full-examples)

**Success with data:**

```
Response::message("Users retrieved")
    ->status(200)
    ->data(["users" => [["id" => 1, "name" => "Alice"]]]);
```

```
{
    "success": true,
    "message": "Users retrieved",
    "data": {"users": [{"id": 1, "name": "Alice"}]},
    "status": [200, "OK"]
}
```

**Validation error:**

```
Response::error("Validation failed", 422)
    ->data(["email" => "The email field is required."], "errors");
```

```
{
    "success": false,
    "message": "Validation failed",
    "errors": {"email": "The email field is required."},
    "status": [422, "Unprocessable Content"]
}
```

**Minimal response:**

```
Response::status(204)->hideMessage()->hideData();
```

```
{
    "success": true,
    "status": [204, "No Content"]
}
```

Supported HTTP Status Codes
---------------------------

[](#supported-http-status-codes)

All standard HTTP status codes (1xx through 5xx) are supported with their official reason phrases. Custom status messages can be provided via `->status($code, "Your message")`.

Testing
-------

[](#testing)

Tests run automatically via GitHub Actions on every push to `main`/`develop` and on pull requests, across PHP 8.1 through 8.4.

To run locally:

```
composer install
vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Contributions are welcome. Please submit a pull request or open an issue for bugs and suggestions. Follow [Conventional Commits](https://www.conventionalcommits.org) for commit messages.

License
-------

[](#license)

MIT — see [LICENSE](LICENSE) for details.

Author
------

[](#author)

[Alireza Javadi](mailto:e@alirezajawadi.ir)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance73

Regular maintenance activity

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

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.

###  Release Activity

Cadence

Every ~37 days

Recently: every ~77 days

Total

11

Last Release

364d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ab5aea8b6404a2cbb786c04441060d73c0b1f683f5009214e67e9bff13e4fcc?d=identicon)[alirezajavadi](/maintainers/alirezajavadi)

---

Top Contributors

[![alirezajavadigit](https://avatars.githubusercontent.com/u/145116978?v=4)](https://github.com/alirezajavadigit "alirezajavadigit (67 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alirezajavadi-jsonize/health.svg)

```
[![Health](https://phpackages.com/badges/alirezajavadi-jsonize/health.svg)](https://phpackages.com/packages/alirezajavadi-jsonize)
```

PHPackages © 2026

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