PHP Classes


Recommend this page to a friend!
  Classes of Josantonius   PHP JSON   Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Load and save variable values to JSON files
Author: By
Last change: Updated to 1.1.7 version
Updated to 1.1.7 version
Updated to 1.1.7 version
Date: 5 years ago
Size: 7,122 bytes


Class file image Download

PHP Json library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

PHP simple library for managing Json files.


This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.


The preferred way to install this extension is through Composer.

To install PHP Json library, simply:

$ composer require Josantonius/Json

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

$ composer require Josantonius/Json --prefer-source

You can also clone the complete repository with Git:

$ git clone

Or install it manually:

Download Json.php, JsonLastError.php and JsonException.php:

$ wget
$ wget
$ wget

Available Methods

Available methods in this library:

- Creating JSON file from array:

Json::arrayToFile($array, $file);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $array | Array to be converted to JSON. | array | Yes | | | $file | Path to the file. | string | Yes | |

# Return (boolean)

- Save to array the JSON file content:


| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $file | Path or external url to JSON file. | string | Yes | |

# Return (array|false)

- Check for errors:


# Return (array|null) ? Null if there are no errors or array with state code and error message.

- Get collection of JSON errors:


# Return (array) ? Collection of JSON errors.

Quick Start

To use this library with Composer:

require __DIR__ . '/vendor/autoload.php';

use Josantonius\Json\Json;

Or If you installed it manually, use it:

require_once __DIR__ . '/Json.php';
require_once __DIR__ . '/JsonLastError.php';
require_once __DIR__ . '/JsonException.php';

use Josantonius\Json\Json;


Example of use for this library:

- Creating JSON file from array:

$array = [
	'name'  => 'Josantonius',
    'email' => '',
    'url'   => ''

$pathfile = __DIR__ . '/filename.json';

Json::arrayToFile($array, $pathfile);

- Save to array the JSON file content:

$pathfile = __DIR__ . '/filename.json';

$array = Json::fileToArray($pathfile);

- Check for errors:

$lastError = JsonLastError::check();

if (!is_null($lastError)) {

- Get collection of JSON errors:

$jsonLastErrorCollection = JsonLastError::getCollection();


To run tests you just need composer and to execute the following:

$ git clone

$ cd PHP-Json

$ composer install

Run unit tests with PHPUnit:

$ composer phpunit

Run PSR2 code standard tests with PHPCS:

$ composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:

$ composer phpmd

Run all previous tests:

$ composer tests

Exception Handler

This library uses exception handler that you can customize.


  • [ ] Add new feature.
  • [ ] Improve tests.
  • [ ] Improve documentation.
  • [ ] Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.


If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command `composer install` to install the dependencies. This will also install the dev dependencies.
  • Run the command `composer fix` to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Thank you to all the people who already contributed to this project!

<img alt="peter279k" src="" height="117" width="117"> | :---:| peter279k|


The file structure from this repository was created with PHP-Skeleton.


This project is licensed under MIT license. See the LICENSE file for more info.


2016 - 2018 Josantonius,

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.