{"id":16329276,"url":"https://github.com/josantonius/php-json","last_synced_at":"2025-03-20T22:32:03.826Z","repository":{"id":37831838,"uuid":"76500116","full_name":"josantonius/php-json","owner":"josantonius","description":"PHP simple library for managing JSON files","archived":false,"fork":false,"pushed_at":"2023-01-11T05:29:29.000Z","size":1266,"stargazers_count":14,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T18:33:44.286Z","etag":null,"topics":["composer","json","php","php-json"],"latest_commit_sha":null,"homepage":"https://josantonius.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josantonius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["Josantonius"]}},"created_at":"2016-12-14T21:53:59.000Z","updated_at":"2024-12-29T14:11:55.000Z","dependencies_parsed_at":"2023-02-09T01:45:52.060Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-json","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244704045,"owners_count":20496231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","json","php","php-json"],"created_at":"2024-10-10T23:14:58.638Z","updated_at":"2025-03-20T22:32:03.456Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"readme":"# PHP JSON library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/Json/v/stable)](https://packagist.org/packages/josantonius/json)\n[![License](https://poser.pugx.org/josantonius/json/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/json/downloads)](https://packagist.org/packages/josantonius/json)\n[![CI](https://github.com/josantonius/php-json/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-json/actions/workflows/ci.yml)\n[![CodeCov](https://codecov.io/gh/josantonius/php-json/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-json)\n[![PSR1](https://img.shields.io/badge/PSR-1-f57046.svg)](https://www.php-fig.org/psr/psr-1/)\n[![PSR4](https://img.shields.io/badge/PSR-4-9b59b6.svg)](https://www.php-fig.org/psr/psr-4/)\n[![PSR12](https://img.shields.io/badge/PSR-12-1abc9c.svg)](https://www.php-fig.org/psr/psr-12/)\n\nPHP simple library for managing JSON files.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [Json Class](#json-class)\n- [Exceptions Used](#exceptions-used)\n- [Usage](#usage)\n- [Tests](#tests)\n- [TODO](#todo)\n- [Changelog](#changelog)\n- [Contribution](#contribution)\n- [Sponsor](#sponsor)\n- [License](#license)\n\n---\n\n## Requirements\n\n- Operating System: Linux.\n\n- PHP versions: 8.1 | 8.2.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP JSON library**, simply:\n\n```console\ncomposer require josantonius/json\n```\n\nThe previous command will only install the necessary files,\nif you prefer to **download the full source code** use:\n\n```console\ncomposer require josantonius/json --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-json.git\n```\n\n## Available Classes\n\n### Json Class\n\n`Josantonius\\Json\\Json`\n\nCreate object referencing the JSON file:\n\n```php\n/**\n * @param string $filepath The path to the JSON file to be handled.\n */\npublic function __construct(public readonly string $filepath)\n{\n}\n```\n\nGet the path to the JSON file:\n\n```php\npublic readonly string $filepath;\n```\n\nCheck if the JSON file has already been created:\n\n```php\n/**\n * @return bool True if the file exists at the specified filepath, false otherwise.\n */\npublic function exists(): bool;\n```\n\nGet the contents of the JSON file:\n\n```php\n/**\n * @param bool $asObject If true and the value is an array, it is returned as an object.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n *\n * @return mixed the contents of the JSON file.\n */\npublic function get(bool $asObject = false): mixed;\n```\n\nSet the contents of a JSON or a key within the file:\n\n```php\n/**\n * @param mixed  $content The data that will be written to the file or a key within the file.\n * @param string $dot     The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws CreateFileException\n * @throws CreateDirectoryException\n * @throws NoIterableElementException\n *\n * @return mixed the content of the JSON file after the set operation.\n */\npublic function set(mixed $content = [], string $dot = null): array|bool|int|null|string;\n```\n\nMerge the provided data with the contents of a JSON file or a key within the file:\n\n```php\n/**\n * @param mixed  $content The data that will be written to the file or a key within the file.\n * @param string $dot     The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n * @throws NoIterableElementException\n *\n * @return mixed the content of the JSON file after the merge operation.\n */\npublic function merge(array|object $content, string $dot = null): array;\n```\n\nRemove and get the last element of a JSON file or a key within the file:\n\n```php\n/**\n * @param string $dot The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n * @throws NoIterableElementException\n *\n * @return mixed|null the last value of JSON file, or null if array is empty.\n */\npublic function pop(string $dot = null): mixed;\n```\n\nAdd the provided data to the end of the contents of a JSON file or a key within the file:\n\n```php\n/**\n * @param mixed  $content The data that will be written to the file or a key within the file.\n * @param string $dot     The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n * @throws NoIterableElementException\n *\n * @return mixed the content of the JSON file after the push operation.\n */\npublic function push(mixed $content, string $dot = null): array;\n```\n\nRemove and get the first element of a JSON file or a key within the file:\n\n```php\n/**\n * @param string $dot The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n * @throws NoIterableElementException\n *\n * @return mixed|null the shifted value, or null if array is empty.\n */\npublic function shift(string $dot = null): mixed(mixed $content, string $dot = null): array;\n```\n\nRemove a key and its value from the contents of a JSON file:\n\n```php\n/**\n * @param string $dot       The dot notation representing the key to be modified within the file.\n * @param bool   $reindexed If true, the array will be re-indexed.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n *\n * @return array the content of the JSON file after the unset operation.\n */\npublic function unset(string $dot, bool $reindexed = false): array;\n```\n\nAdd the provided data to the beginning of the contents of a JSON file or a key within the file:\n\n```php\n/**\n * @param mixed  $content The data that will be written to the file or a key within the file.\n * @param string $dot     The dot notation representing the key to be modified within the file.\n *\n * @throws GetFileException\n * @throws JsonErrorException\n * @throws NoIterableFileException\n * @throws NoIterableElementException\n *\n * @return mixed the content of the JSON file after the unshift operation.\n */\npublic function unshift(mixed $content, string $dot = null): mixed;\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\Json\\Exceptions\\GetFileException;           // if file reading failed\nuse Josantonius\\Json\\Exceptions\\CreateFileException;        // if file creation failed\nuse Josantonius\\Json\\Exceptions\\JsonErrorException;         // if the file contains invalid JSON\nuse Josantonius\\Json\\Exceptions\\NoIterableFileException;    // if the file isn't a JSON array\nuse Josantonius\\Json\\Exceptions\\CreateDirectoryException;   // if directory creation failed\nuse Josantonius\\Json\\Exceptions\\NoIterableElementException; // if $dot isn't an array location\n```\n\n## Usage\n\nExample of use for this library:\n\n### Get the path of the JSON file\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003efilepath; // 'file.json'\n```\n\n### Check whether a local file exists\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eexists(); // bool\n```\n\n### Get the JSON file contents as array\n\n**`file.json`**\n\n```json\n{\n    \"foo\": \"bar\"\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eget(); // ['foo' =\u003e 'bar']\n```\n\n### Get the JSON file contents as object\n\n**`file.json`**\n\n```json\n{\n    \"foo\": \"bar\"\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eget(asObject: true); // object(stdClass) { [\"foo\"] =\u003e string(3) \"bar\" }\n```\n\n### Set an empty array in the JSON file contents\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eset();\n```\n\n**`file.json`**\n\n```json\n[]\n```\n\n### Set the contents of a JSON file\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eset(['foo' =\u003e 'bar']);\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": \"bar\"\n}\n```\n\n### Set the contents of a key within the JSON file using dot notation\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": []\n    }\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eset('baz', 'foo.bar.0');\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            \"baz\"\n        ]\n    }\n}\n```\n\n### Merge the provided data with the contents of the JSON file\n\n**`file.json`**\n\n```json\n{\n    \"foo\": \"bar\"\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003emerge(['bar' =\u003e 'foo']);\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": \"bar\",\n    \"bar\": \"foo\"\n}\n```\n\n### Merge the provided data with the contents of a key within the file using dot notation\n\n**`file.json`**\n\n```json\n{\n    \"foo\": [\n        {\n            \"bar\": \"baz\"\n        }\n    ]\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003emerge(['baz' =\u003e 'bar'], 'foo.0');\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": [\n        {\n            \"bar\": \"baz\",\n            \"baz\": \"bar\"\n        }\n    ]\n}\n```\n\n### Remove and get the last element of a JSON file\n\n**`file.json`**\n\n```json\n[\n    1,\n    2,\n    3\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003epop(); // 3\n```\n\n**`file.json`**\n\n```json\n[\n    1,\n    2\n]\n```\n\n### Remove and get the last element of a key within the file using dot notation\n\n**`file.json`**\n\n```json\n{\n    \"foo\": [\n        1,\n        2,\n        3\n    ]\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003epop(); // 3\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": [\n        1,\n        2\n    ]\n}\n```\n\n### Add the provided data to the end of the contents of a JSON file\n\n**`file.json`**\n\n```json\n[\n    {\n        \"name\": \"foo\"\n    }\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003epush(['name'  =\u003e 'bar']);\n```\n\n**`file.json`**\n\n```json\n[\n    {\n        \"name\": \"foo\"\n    },\n    {\n        \"name\": \"bar\"\n    }\n]\n```\n\n### Add provided data to the end of the contents of a key within the file using dot notation\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            []\n        ]\n    }\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003epush('baz', 'foo.bar.0');\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            [\n                \"baz\"\n            ]\n        ]\n    }\n}\n```\n\n### Remove and get the first element of the contents of a JSON file\n\n**`file.json`**\n\n```json\n[\n    1,\n    2,\n    3\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eshift(); // 1\n```\n\n**`file.json`**\n\n```json\n[\n    2,\n    3\n]\n```\n\n### Remove and get the first item of the contents of a key within the file using dot notation\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            [\n                1\n            ]\n        ]\n    }\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eshift('foo.bar.0'); // 1\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            []\n        ]\n    }\n}\n```\n\n### Remove a string key and its value from the contents of a JSON file\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            []\n        ]\n    }\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eunset('foo.bar');\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": []\n}\n```\n\n### Remove a numeric key and its value from the contents of a JSON file\n\n**`file.json`**\n\n```json\n[\n    1,\n    2,\n    3\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eunset('1');\n```\n\n**`file.json`**\n\n```json\n{\n    \"0\": 1,\n    \"2\": 3\n}\n```\n\n### Remove a numeric key and its value from the contents of a JSON file and re-index it\n\n**`file.json`**\n\n```json\n[\n    1,\n    2,\n    3\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eunset('1', reindexed: true);\n```\n\n**`file.json`**\n\n```json\n[\n    1,\n    3\n]\n```\n\n### Add the provided data to the beginning of the contents of a JSON file\n\n**`file.json`**\n\n```json\n[\n    1,\n    2,\n    3\n]\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eunshift(0);\n```\n\n**`file.json`**\n\n```json\n[\n    0,\n    1,\n    2,\n    3\n]\n```\n\n### Add the provided data to the beginning of the contents of a key within the file using dot\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            [\n                1\n            ]\n        ]\n    }\n}\n```\n\n**`index.php`**\n\n```php\nuse Josantonius\\Json\\Json;\n\n$json = new Json('file.json');\n\n$json-\u003eunshift(0, 'foo.bar.0');\n```\n\n**`file.json`**\n\n```json\n{\n    \"foo\": {\n        \"bar\": [\n            [\n                0,\n                1\n            ]\n        ]\n    }\n}\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/)\nand to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-json.git\n```\n\n```console\ncd php-json\n```\n\n```console\ncomposer install\n```\n\nRun unit tests with [PHPUnit](https://phpunit.de/):\n\n```console\ncomposer phpunit\n```\n\nRun code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```console\ncomposer phpcs\n```\n\nRun [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:\n\n```console\ncomposer phpmd\n```\n\nRun all previous tests:\n\n```console\ncomposer tests\n```\n\n## TODO\n\n- [ ] Add new feature\n- [ ] Improve tests\n- [ ] Improve documentation\n- [ ] Improve English translation in the README file\n- [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)\n\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-json/releases).\n\n## Contribution\n\nPlease make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull\nrequest, start a discussion or report a issue.\n\nThanks to all [contributors](https://github.com/josantonius/php-json/graphs/contributors)! :heart:\n\n## Sponsor\n\nIf this project helps you to reduce your development time,\n[you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work :blush:\n\n## License\n\nThis repository is licensed under the [MIT License](LICENSE).\n\nCopyright © 2016-present, [Josantonius](https://github.com/josantonius#contact)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-json/lists"}