{"id":17045711,"url":"https://github.com/remorhaz/php-json-pointer","last_synced_at":"2025-04-12T15:31:28.793Z","repository":{"id":60774648,"uuid":"60965154","full_name":"remorhaz/php-json-pointer","owner":"remorhaz","description":"JSON Pointer (RFC-6901) PHP implementation","archived":false,"fork":false,"pushed_at":"2024-02-20T09:53:20.000Z","size":321,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T10:11:24.477Z","etag":null,"topics":["json","json-pointer","php","rfc-6901"],"latest_commit_sha":null,"homepage":null,"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/remorhaz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-12T12:05:05.000Z","updated_at":"2025-01-01T13:21:32.000Z","dependencies_parsed_at":"2022-10-04T15:46:26.897Z","dependency_job_id":null,"html_url":"https://github.com/remorhaz/php-json-pointer","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remorhaz%2Fphp-json-pointer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remorhaz%2Fphp-json-pointer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remorhaz%2Fphp-json-pointer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remorhaz%2Fphp-json-pointer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remorhaz","download_url":"https://codeload.github.com/remorhaz/php-json-pointer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248170202,"owners_count":21059156,"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":["json","json-pointer","php","rfc-6901"],"created_at":"2024-10-14T09:38:10.461Z","updated_at":"2025-04-12T15:31:28.239Z","avatar_url":"https://github.com/remorhaz.png","language":"PHP","readme":"# PHP JSON Pointer\n\n[![Latest Stable Version](https://poser.pugx.org/remorhaz/php-json-pointer/v/stable)](https://packagist.org/packages/remorhaz/php-json-pointer)\n[![Build](https://github.com/remorhaz/php-json-pointer/actions/workflows/build.yml/badge.svg)](https://github.com/remorhaz/php-json-pointer/actions/workflows/build.yml)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/remorhaz/php-json-pointer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/remorhaz/php-json-pointer/?branch=master)\n[![codecov](https://codecov.io/gh/remorhaz/php-json-pointer/branch/master/graph/badge.svg)](https://codecov.io/gh/remorhaz/php-json-pointer)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fremorhaz%2Fphp-json-pointer%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/remorhaz/php-json-pointer/master)\n[![Total Downloads](https://poser.pugx.org/remorhaz/php-json-pointer/downloads)](https://packagist.org/packages/remorhaz/php-json-pointer)\n[![License](https://poser.pugx.org/remorhaz/php-json-pointer/license)](https://packagist.org/packages/remorhaz/php-json-pointer)\n\nThis library implements [RFC6901](https://tools.ietf.org/html/rfc6901)-compliant JSON pointers.\n\n## Requirements\n* PHP 8.1\n\n## Features\n* Selecting part of a JSON document.\n* Removing part of a JSON document.\n* Replacing/adding part of a JSON document.\n\n# Installation\nYou will need [composer](https://getcomposer.org) to perform install.\n```\ncomposer require remorhaz/php-json-pointer\n```\n\n# Documentation\n## Accessing JSON document\nYou can create accessible JSON document either from encoded JSON string or from decoded JSON data using corresponding _node value factory_:\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Data\\Value\\EncodedJson;\nuse Remorhaz\\JSON\\Data\\Value\\DecodedJson;\n\n// Creating document from JSON-encoded string:\n$encodedValueFactory = EncodedJson\\NodeValueFactory::create();\n$encodedJson = '{\"a\":1}';\n$document1 = $encodedValueFactory-\u003ecreateValue($encodedJson);\n\n// Creating document from decoded JSON data:\n$decodedValueFactory = DecodedJson\\NodeValueFactory::create();\n$decodedJson = (object) ['a' =\u003e 1];\n$document2 = $decodedValueFactory-\u003ecreateValue($decodedJson);\n``` \n\n## Creating query\nYou should use _query factory_ to create query from JSON Pointer expression:\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Pointer\\Query\\QueryFactory;\n\n$queryFactory = QueryFactory::create();\n\n// Creating query that selects 'a' property from document:\n$query = $queryFactory-\u003ecreateQuery('/a');\n```\n\n## Processing query\nYou should use an instance of _query processor_ to execute queries on given JSON documents:\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Pointer\\Processor\\Processor;\n\n$processor = Processor::create();\n```\n\n### Selecting part of a JSON document\nTo get part of a JSON document use `::select()` method.\n\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Data\\Value\\EncodedJson\\NodeValueFactory;\nuse Remorhaz\\JSON\\Pointer\\Processor\\Processor;\nuse Remorhaz\\JSON\\Pointer\\Query\\QueryFactory;\n\n$nodeValueFactory = NodeValueFactory::create();\n$queryFactory = QueryFactory::create();\n$processor = Processor::create();\n\n$document = $nodeValueFactory-\u003ecreateValue('{\"a\":\"b\"}');\n\n// Selecting existing value\n$query1 = $queryFactory-\u003ecreateQuery('/a');\n$result1 = $processor-\u003eselect($query1, $document);\nvar_dump($result1-\u003eexists()); // boolean: true\nvar_dump($result1-\u003edecode()); // string: 'b'\nvar_dump($result1-\u003eencode()); // string: '\"b\"'\n\n// Attempting to select non-existing value\n$query2 = $queryFactory-\u003ecreateQuery('/c');\n$result2 = $processor-\u003eselect($query2, $document);\nvar_dump($result2-\u003eexists()); // boolean: false\nvar_dump($result2-\u003edecode()); // throws an exception\n``` \nNote that you can either encode result of a selection to JSON string or decode them to raw PHP data. Before accessing a result of `::select()` you can check it's existence with `::exists()` method to avoid exception.\n\n### Deleting part of a JSON document\nTo delete part of a JSON document use `::delete()` method.\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Data\\Value\\EncodedJson\\NodeValueFactory;\nuse Remorhaz\\JSON\\Pointer\\Processor\\Processor;\nuse Remorhaz\\JSON\\Pointer\\Query\\QueryFactory;\n\n$nodeValueFactory = NodeValueFactory::create();\n$queryFactory = QueryFactory::create();\n$processor = Processor::create();\n\n$document = $nodeValueFactory-\u003ecreateValue('{\"a\":\"b\",\"c\":\"d\"}');\n\n// Deleting existing value\n$query1 = $queryFactory-\u003ecreateQuery('/a');\n$result1 = $processor-\u003edelete($query1, $document);\nvar_dump($result1-\u003eexists()); // boolean: true\nvar_dump($result1-\u003eencode()); // string: '{\"c\":\"d\"}'\n\n// Attempting to delete non-existing value\n$query2 = $queryFactory-\u003ecreateQuery('/e');\n$result2 = $processor-\u003edelete($query2, $document);\nvar_dump($result2-\u003eexists()); // boolean: false\nvar_dump($result2-\u003eencode()); // throws an exception\n```\nNote that `::delete()` function returns non-existing result if query points to non-existing value.\n\n### Replacing part of a JSON document\nTo replace part of a JSON document use `::replace()` method.\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Data\\Value\\EncodedJson\\NodeValueFactory;\nuse Remorhaz\\JSON\\Pointer\\Processor\\Processor;\nuse Remorhaz\\JSON\\Pointer\\Query\\QueryFactory;\n\n$nodeValueFactory = NodeValueFactory::create();\n$queryFactory = QueryFactory::create();\n$processor = Processor::create();\n\n$document = $nodeValueFactory-\u003ecreateValue('{\"a\":\"b\",\"c\":\"d\"}');\n$replacement = $nodeValueFactory-\u003ecreateValue('\"e\"');\n\n// Replacing existing value\n$query1 = $queryFactory-\u003ecreateQuery('/a');\n$result1 = $processor-\u003ereplace($query1, $document, $replacement);\nvar_dump($result1-\u003eexists()); // boolean: true\nvar_dump($result1-\u003eencode()); // string: '{\"a\":\"e\",\"c\":\"d\"}'\n\n// Attempting to replace non-existing value\n$query2 = $queryFactory-\u003ecreateQuery('/f');\n$result2 = $processor-\u003ereplace($query2, $document, $replacement);\nvar_dump($result2-\u003eexists()); // boolean: false\nvar_dump($result2-\u003eencode()); // throws an exception\n```\n\n### Adding part of a JSON document\nTo add part of a JSON document use `::add()` method.\n```php\n\u003c?php\nuse Remorhaz\\JSON\\Data\\Value\\EncodedJson\\NodeValueFactory;\nuse Remorhaz\\JSON\\Pointer\\Processor\\Processor;\nuse Remorhaz\\JSON\\Pointer\\Query\\QueryFactory;\n\n$nodeValueFactory = NodeValueFactory::create();\n$queryFactory = QueryFactory::create();\n$processor = Processor::create();\n\n// Working with object\n$document1 = $nodeValueFactory-\u003ecreateValue('{\"a\":\"b\",\"c\":\"d\"}');\n$replacement1 = $nodeValueFactory-\u003ecreateValue('\"e\"');\n\n// Replacing existing property\n$query1 = $queryFactory-\u003ecreateQuery('/a');\n$result1 = $processor-\u003eadd($query1, $document1, $replacement1);\nvar_dump($result1-\u003eexists()); // boolean: true\nvar_dump($result1-\u003eencode()); // string: '{\"a\":\"e\",\"c\":\"d\"}'\n\n// Adding non-existing property\n$query2 = $queryFactory-\u003ecreateQuery('/f');\n$result2 = $processor-\u003eadd($query2, $document1, $replacement1);\nvar_dump($result2-\u003eexists()); // boolean: true\nvar_dump($result2-\u003eencode()); // string: '{\"a\":\"b\",\"c\":\"d\",\"f\":\"e\"}'\n\n// Adding non-existing property\n$query2 = $queryFactory-\u003ecreateQuery('/f');\n$result2 = $processor-\u003eadd($query2, $document1, $replacement1);\nvar_dump($result2-\u003eexists()); // boolean: true\nvar_dump($result2-\u003eencode()); // string: '{\"a\":\"b\",\"c\":\"d\",\"f\":\"e\"}'\n\n// Attempting to add property to non-existing object\n$query3 = $queryFactory-\u003ecreateQuery('/e/f');\n$result3 = $processor-\u003eadd($query3, $document1, $replacement1);\nvar_dump($result3-\u003eexists()); // boolean: false\nvar_dump($result3-\u003eencode()); // throws an exception\n\n// Working with array\n$document2 = $nodeValueFactory-\u003ecreateValue('[1,2]');\n$replacement2 = $nodeValueFactory-\u003ecreateValue('3');\n\n// Inserting new element before given index\n$query4 = $queryFactory-\u003ecreateQuery('/1');\n$result4 = $processor-\u003eadd($query4, $document2, $replacement2);\nvar_dump($result4-\u003eexists()); // boolean: true\nvar_dump($result4-\u003eencode()); // string: '[1,3,2]'\n\n// Appending new element to the end of array\n$query5 = $queryFactory-\u003ecreateQuery('/-');\n$result5 = $processor-\u003eadd($query5, $document2, $replacement2);\nvar_dump($result5-\u003eexists()); // boolean: true\nvar_dump($result5-\u003eencode()); // string: '[1,2,3]'\n```\nIf query points to existing property it will be replaced. Note that you can add values only to existing objects/arrays.\n\n# License\nPHP JSON Pointer is licensed under MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremorhaz%2Fphp-json-pointer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremorhaz%2Fphp-json-pointer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremorhaz%2Fphp-json-pointer/lists"}