PHPackages                             nannehuiges/jsend - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. nannehuiges/jsend

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

nannehuiges/jsend
=================

A simple PHP implementation of the JSend specification.

v6.0.1(2mo ago)31368.1k—2.2%52BSD-3-ClausePHPPHP &gt;=8.1CI passing

Since Apr 11Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/NanneHuiges/JSend)[ Packagist](https://packagist.org/packages/nannehuiges/jsend)[ Docs](https://github.com/NanneHuiges/jsend)[ RSS](/packages/nannehuiges-jsend/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (4)Versions (15)Used By (2)

[![Build Status](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php81.yml/badge.svg)](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php81.yml/badge.svg)[![Build Status](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php82.yml/badge.svg)](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php82.yml/badge.svg)[![Build Status](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php83.yml/badge.svg)](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpunit_php83.yml/badge.svg)[![Build Status](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpmd.yml/badge.svg)](https://github.com/NanneHuiges/JSend/actions/workflows/ci_phpmd.yml/badge.svg)

[![Code Climate](https://camo.githubusercontent.com/8a0477cd254fc23abd34b5247ab623bd56fdc0f1e6703b4c7363cf42a1234a47/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f4e616e6e654875696765732f4a53656e642f6261646765732f6770612e737667)](https://codeclimate.com/github/NanneHuiges/JSend)
[![Issue Count](https://camo.githubusercontent.com/25a323f4a7ff9d3f95c00951b3726a05ea99d669eb04b6e296f7d43a87df3846/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f4e616e6e654875696765732f4a53656e642f6261646765732f69737375655f636f756e742e737667)](https://codeclimate.com/github/NanneHuiges/JSend)

[![Total Downloads](https://camo.githubusercontent.com/1ffc04db4feaf79b70d642c901e657181f3e491ef7bcaf85f312f2ee4d28da39/68747470733a2f2f706f7365722e707567782e6f72672f6e616e6e656875696765732f6a73656e642f646f776e6c6f616473)](https://packagist.org/packages/nannehuiges/jsend)

JSend
=====

[](#jsend)

A simple PHP implementation of the [JSend specification](https://github.com/omniti-labs/jsend).

Usage
-----

[](#usage)

```
use JSend\JSendResponse;
```

### New response

[](#new-response)

```
$success = new JSendResponse('success', $data);
$fail = new JSendResponse('fail', $data);
$error = new JSendResponse('error', $data, 'Not cool.', 9001);
```

```
$success = JSendResponse::success($data);
$fail = JSendResponse::fail($data);
$error = JSendResponse::error('Not cool.', 9001, $data);
```

**Note**: an `InvalidJSendException` is thrown if the status is invalid or if you're creating an `error` without a `message`.

### Convert JSendResponse to JSON

[](#convert-jsendresponse-to-json)

`__toString()` is overridden to encode JSON automatically.

```
$json = $success->encode();
$json = (string) $success;
```

As JSendResponse is `JsonSerializable`, you can use the object directly in `json_encode`

```
json_encode($success);
```

#### Setting flags

[](#setting-flags)

You can set flags if needed:

```
$success->setEncodingOptions(\JSON_PRETTY_PRINT | \JSON_BIGINT_AS_STRING);
$json = $success->encode();
```

### Convert JSON to JSendResponse

[](#convert-json-to-jsendresponse)

```
try {
    $response = JSendResponse::decode($json);
} catch (InvalidJSendException $e) {
    echo "You done gone passed me invalid JSend.";
}
```

### Send JSON as HTTP Response

[](#send-json-as-http-response)

This sets the `Content-Type` header to `application/json` and spits out the JSON.

```
$jsend = new JSendResponse('success', $data);
$jsend->respond();
```

### Get info

[](#get-info)

```
$isSuccess = $response->isSuccess();
$isError = $response->isError();
$isFail = $response->isFail();
$status = $response->getStatus();
$data = $response->getData();
$array = $response->asArray();
```

Additionally, you can call the following methods on an error. A `BadMethodCallException` is thrown if the status is not `error`, so check first.

```
if ($response->isError()) {
    $code = $response->getErrorCode;
    $message = $response->getErrorMessage;
}
```

Development
===========

[](#development)

For your convenience, there is a dockerfile with the right dependencies (php, composer) available. Please use those to run various things (composer, phpunit, etc). You will need `docker` installed, but you don't need `PHP` or `composer` or any of the other dependencies.

Setting up and using a local environment
----------------------------------------

[](#setting-up-and-using-a-local-environment)

To start using the local environment for testing and debugging all you have to is open a shell in the root folder of where this project is checked out. Then run the following command.

```
make build install
```

This command should be run occasionally to keep the local environment up to date. For instance when the composer dependencies are changed.

### Using the shell

[](#using-the-shell)

To open a shell in the docker container run the following command.

```
make shell
```

Available commands are in `/bin`

### Running the code quality tools locally

[](#running-the-code-quality-tools-locally)

We use a variety of tools to keep the code quality of the library high. To run one the tools you only need to run

```
make
```

Available tools:

- `phpstan` [PHPStan](https://phpstan.org/) is static analyser tool that can detect various code issues.
- `phpunit` [PHPUnit](https://phpunit.de/) is the unit testing framework we use for this library.
- `codeclimate` [CodeClimate](https://codeclimate.com/github/NanneHuiges/JSend)

Notes
-----

[](#notes)

- Note that the `composer.lock` file is ignored.

Credits
=======

[](#credits)

The library was written by [Jamie Schembri](https://github.com/shkm). It has been transfered to the current account [Nanne Huiges](https://github.com/NanneHuiges) in december 2015.

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance84

Actively maintained with recent releases

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 84% 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 ~427 days

Recently: every ~696 days

Total

12

Last Release

82d ago

Major Versions

v1.2.1 → v2.0.02015-12-15

v2.1.0 → v3.0.02018-07-11

v3.0.0 → v4.0.02019-12-09

v4.0.0 → v5.0.02021-04-02

v5.0.0 → v6.0.02023-12-15

PHP version history (6 changes)v1.0.0PHP &gt;=5.3.0

v2.0.0PHP &gt;=5.4.0

v3.0.0PHP &gt;=7.0

v4.0.0PHP &gt;=7.2

v5.0.0PHP &gt;=7.4

v6.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/92a268cc64f5931e39561a5f586d4c8ea0a06f5f0042c591e3a78852de23de71?d=identicon)[nanne](/maintainers/nanne)

---

Top Contributors

[![NanneHuiges](https://avatars.githubusercontent.com/u/1526794?v=4)](https://github.com/NanneHuiges "NanneHuiges (89 commits)")[![shkm](https://avatars.githubusercontent.com/u/22677?v=4)](https://github.com/shkm "shkm (11 commits)")[![jeremykendall](https://avatars.githubusercontent.com/u/288613?v=4)](https://github.com/jeremykendall "jeremykendall (3 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")[![jorritidsardi](https://avatars.githubusercontent.com/u/217586559?v=4)](https://github.com/jorritidsardi "jorritidsardi (1 commits)")[![Kalipso0505](https://avatars.githubusercontent.com/u/3777168?v=4)](https://github.com/Kalipso0505 "Kalipso0505 (1 commits)")

---

Tags

phpjsonjsend

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/nannehuiges-jsend/health.svg)

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

###  Alternatives

[adhocore/json-fixer

Fix/repair truncated JSON data

51543.2k2](/packages/adhocore-json-fixer)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[blancks/fast-jsonpatch-php

Class designed to efficiently handle JSON Patch operations in accordance with the RFC 6902 specification

396.4k](/packages/blancks-fast-jsonpatch-php)[josantonius/json

PHP simple library for managing Json files.

1621.6k10](/packages/josantonius-json)

PHPackages © 2026

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