PHPackages                             aalfiann/json-class-php - 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. aalfiann/json-class-php

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

aalfiann/json-class-php
=======================

A class for handle json in a better way.

1.3.1(7y ago)312012MITPHPPHP &gt;=5.4

Since Sep 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/aalfiann/json-class-php)[ Packagist](https://packagist.org/packages/aalfiann/json-class-php)[ RSS](/packages/aalfiann-json-class-php/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (7)Used By (2)

JSON Class PHP
==============

[](#json-class-php)

[![Version](https://camo.githubusercontent.com/75e26d34f8a36609254854891bc0dd4d34a864cb0a65e9d0111e7e759e31a034/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737461626c652d312e332e312d677265656e2e737667)](https://github.com/aalfiann/json-class-php)[![Total Downloads](https://camo.githubusercontent.com/85d847a001f83d4e0f1e208fd44dc8e37c045c70555a905a7634a0d8fc79f395/68747470733a2f2f706f7365722e707567782e6f72672f61616c6669616e6e2f6a736f6e2d636c6173732d7068702f646f776e6c6f616473)](https://packagist.org/packages/aalfiann/json-class-php)[![License](https://camo.githubusercontent.com/4342aed7c96eb79963ffa86cac1da70ac4418e5702bd2ad8bd995577eaeb446b/68747470733a2f2f706f7365722e707567782e6f72672f61616c6669616e6e2f6a736f6e2d636c6173732d7068702f6c6963656e7365)](https://github.com/aalfiann/json-class-php/blob/HEAD/LICENSE.md)

A class for handle json in a better way.

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

[](#installation)

Install this package via [Composer](https://getcomposer.org/).

```
composer require "aalfiann/json-class-php:^1.0"

```

Usage
-----

[](#usage)

### Encode

[](#encode)

```
require_once ('vendor/autoload.php');
use \aalfiann\JSON;

$data = [
    'result'=>'just make a test!',
    'data' => [
        'id' => '1',
        'user' => 'your name',
        'email' => 'your_email@gmail.com',
        'website' => 'http://yourdomain.com',
        'non-utf8' => 'السلام علیکم ورحمة الله وبرکاته!'
    ]
];

header('Content-Type: application/json');
$json = new JSON;
echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
```

### Decode

[](#decode)

```
require_once ('vendor/autoload.php');
use \aalfiann\JSON;

$data = '{"result":"just make a test!","data":{"id":"1","user":"your name","email":"your_email@gmail.com","website":"http://yourdomain.com","non-utf8":"\u00d8\u00a7\u00d9\u0084\u00d8\u00b3\u00d9\u0084\u00d8\u00a7\u00d9\u0085 \u00d8\u00b9\u00d9\u0084\u00db\u008c\u00da\u00a9\u00d9\u0085 \u00d9\u0088\u00d8\u00b1\u00d8\u00ad\u00d9\u0085\u00d8\u00a9 \u00d8\u00a7\u00d9\u0084\u00d9\u0084\u00d9\u0087 \u00d9\u0088\u00d8\u00a8\u00d8\u00b1\u00da\u00a9\u00d8\u00a7\u00d8\u00aa\u00d9\u0087!"},"logger":{"timestamp":"2018-09-17 13:53:12","uniqid":"5b9f95a812c0f"}}';

$json = new JSON;
echo var_dump($json->decode($data,true));
```

### Debug

[](#debug)

You can easily debug with set property `debug` to `true`.

```
require_once ('vendor/autoload.php');
use \aalfiann\JSON;

header('Content-Type: application/json');
$json = new JSON;
$json->debug=true;
echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
```

Chain Usage Example
-------------------

[](#chain-usage-example)

```
require_once ('vendor/autoload.php');
use \aalfiann\JSON;

header('Content-Type: application/json');
$json = new JSON;
echo $json->withSanitizer()->withLog()->encode(['user'=>'yourname'],JSON_PRETTY_PRINT);
```

Properties
----------

[](#properties)

- `$withlog=false,$sanitize=false,$ansii=false,$debug=false,$makesimple=false,$trim=false;`

Chain Function
--------------

[](#chain-function)

- **withSanitizer($sanitize=true)** this will sanitize your data array before execute json\_encode.
- **withTrim($trim=true)** this will trimmed your data array or string before execute json\_encode or json\_decode.
- **withLog($withlog=true)** this will append the logger data into your json.
- **setAnsii($ansii=true)** this will make sanitizer works to handle ANSII chars.
- **setDebug($debug=true)** this is for debugging purpose.
- **makeSimple($makesimple=true)** this will hide the additional data in debug output.

Main Function
-------------

[](#main-function)

- **encode($data,$options=0,$depth=512)** encode array or string to json format.
- **decode($json,$assoc=false,$depth=512,$options=0)** decode json string to `stdClass/array`.
- **isValid($json=null)** to determine json string is valid or not.

Helper Function
---------------

[](#helper-function)

- **convertToUTF8($string)** convert string to valid UTF8 chars.
- **convertToUTF8Ansii($string)** convert string to valid UTF8 chars (support ANSII chars).
- **trimValue($string)** trim the data array or string to strip the whitespace or any characters consider to blank value.
- **debug\_encode($string,$options=0,$depth=512)** debugger to test json encode.
- **debug\_decode($json,$assoc=false,$depth=512,$options=0)** debugger to test json decode.
- **errorMessage($jsonlasterror,$content)** case error message about json.
- **fixControlChar($string)** Most common fixed hidden control char in json string which made json decode fails.
- **modifyJsonStringInArray($data,$jsonfield,$setnewfield="")** modify json data string in some field array to be nice json data structure.
- **concatenate($data,$escape=true,$options=0,$depth=512)** concatenate json data
- **minify($json)** Minify the json string

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

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 ~40 days

Recently: every ~50 days

Total

6

Last Release

2594d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c101f6be505e4713be942f6b80d849b46702e6f4069430e1dbbe60fa18289c54?d=identicon)[aalfiann](/maintainers/aalfiann)

---

Top Contributors

[![aalfiann](https://avatars.githubusercontent.com/u/9458941?v=4)](https://github.com/aalfiann "aalfiann (11 commits)")

---

Tags

jsonclass

### Embed Badge

![Health badge](/badges/aalfiann-json-class-php/health.svg)

```
[![Health](https://phpackages.com/badges/aalfiann-json-class-php/health.svg)](https://phpackages.com/packages/aalfiann-json-class-php)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k316.9M612](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15267.7M16](/packages/clue-ndjson-react)

PHPackages © 2026

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