https://github.com/remorhaz/php-json-pointer
JSON Pointer (RFC-6901) PHP implementation
https://github.com/remorhaz/php-json-pointer
json json-pointer php rfc-6901
Last synced: about 2 months ago
JSON representation
JSON Pointer (RFC-6901) PHP implementation
- Host: GitHub
- URL: https://github.com/remorhaz/php-json-pointer
- Owner: remorhaz
- License: mit
- Created: 2016-06-12T12:05:05.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-20T09:53:20.000Z (over 1 year ago)
- Last Synced: 2025-03-26T10:11:24.477Z (2 months ago)
- Topics: json, json-pointer, php, rfc-6901
- Language: PHP
- Size: 313 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP JSON Pointer
[](https://packagist.org/packages/remorhaz/php-json-pointer)
[](https://github.com/remorhaz/php-json-pointer/actions/workflows/build.yml)
[](https://scrutinizer-ci.com/g/remorhaz/php-json-pointer/?branch=master)
[](https://codecov.io/gh/remorhaz/php-json-pointer)
[](https://dashboard.stryker-mutator.io/reports/github.com/remorhaz/php-json-pointer/master)
[](https://packagist.org/packages/remorhaz/php-json-pointer)
[](https://packagist.org/packages/remorhaz/php-json-pointer)This library implements [RFC6901](https://tools.ietf.org/html/rfc6901)-compliant JSON pointers.
## Requirements
* PHP 8.1## Features
* Selecting part of a JSON document.
* Removing part of a JSON document.
* Replacing/adding part of a JSON document.# Installation
You will need [composer](https://getcomposer.org) to perform install.
```
composer require remorhaz/php-json-pointer
```# Documentation
## Accessing JSON document
You can create accessible JSON document either from encoded JSON string or from decoded JSON data using corresponding _node value factory_:
```php
createValue($encodedJson);// Creating document from decoded JSON data:
$decodedValueFactory = DecodedJson\NodeValueFactory::create();
$decodedJson = (object) ['a' => 1];
$document2 = $decodedValueFactory->createValue($decodedJson);
```## Creating query
You should use _query factory_ to create query from JSON Pointer expression:
```php
createQuery('/a');
```## Processing query
You should use an instance of _query processor_ to execute queries on given JSON documents:
```php
createValue('{"a":"b"}');// Selecting existing value
$query1 = $queryFactory->createQuery('/a');
$result1 = $processor->select($query1, $document);
var_dump($result1->exists()); // boolean: true
var_dump($result1->decode()); // string: 'b'
var_dump($result1->encode()); // string: '"b"'// Attempting to select non-existing value
$query2 = $queryFactory->createQuery('/c');
$result2 = $processor->select($query2, $document);
var_dump($result2->exists()); // boolean: false
var_dump($result2->decode()); // throws an exception
```
Note 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.### Deleting part of a JSON document
To delete part of a JSON document use `::delete()` method.
```php
createValue('{"a":"b","c":"d"}');// Deleting existing value
$query1 = $queryFactory->createQuery('/a');
$result1 = $processor->delete($query1, $document);
var_dump($result1->exists()); // boolean: true
var_dump($result1->encode()); // string: '{"c":"d"}'// Attempting to delete non-existing value
$query2 = $queryFactory->createQuery('/e');
$result2 = $processor->delete($query2, $document);
var_dump($result2->exists()); // boolean: false
var_dump($result2->encode()); // throws an exception
```
Note that `::delete()` function returns non-existing result if query points to non-existing value.### Replacing part of a JSON document
To replace part of a JSON document use `::replace()` method.
```php
createValue('{"a":"b","c":"d"}');
$replacement = $nodeValueFactory->createValue('"e"');// Replacing existing value
$query1 = $queryFactory->createQuery('/a');
$result1 = $processor->replace($query1, $document, $replacement);
var_dump($result1->exists()); // boolean: true
var_dump($result1->encode()); // string: '{"a":"e","c":"d"}'// Attempting to replace non-existing value
$query2 = $queryFactory->createQuery('/f');
$result2 = $processor->replace($query2, $document, $replacement);
var_dump($result2->exists()); // boolean: false
var_dump($result2->encode()); // throws an exception
```### Adding part of a JSON document
To add part of a JSON document use `::add()` method.
```php
createValue('{"a":"b","c":"d"}');
$replacement1 = $nodeValueFactory->createValue('"e"');// Replacing existing property
$query1 = $queryFactory->createQuery('/a');
$result1 = $processor->add($query1, $document1, $replacement1);
var_dump($result1->exists()); // boolean: true
var_dump($result1->encode()); // string: '{"a":"e","c":"d"}'// Adding non-existing property
$query2 = $queryFactory->createQuery('/f');
$result2 = $processor->add($query2, $document1, $replacement1);
var_dump($result2->exists()); // boolean: true
var_dump($result2->encode()); // string: '{"a":"b","c":"d","f":"e"}'// Adding non-existing property
$query2 = $queryFactory->createQuery('/f');
$result2 = $processor->add($query2, $document1, $replacement1);
var_dump($result2->exists()); // boolean: true
var_dump($result2->encode()); // string: '{"a":"b","c":"d","f":"e"}'// Attempting to add property to non-existing object
$query3 = $queryFactory->createQuery('/e/f');
$result3 = $processor->add($query3, $document1, $replacement1);
var_dump($result3->exists()); // boolean: false
var_dump($result3->encode()); // throws an exception// Working with array
$document2 = $nodeValueFactory->createValue('[1,2]');
$replacement2 = $nodeValueFactory->createValue('3');// Inserting new element before given index
$query4 = $queryFactory->createQuery('/1');
$result4 = $processor->add($query4, $document2, $replacement2);
var_dump($result4->exists()); // boolean: true
var_dump($result4->encode()); // string: '[1,3,2]'// Appending new element to the end of array
$query5 = $queryFactory->createQuery('/-');
$result5 = $processor->add($query5, $document2, $replacement2);
var_dump($result5->exists()); // boolean: true
var_dump($result5->encode()); // string: '[1,2,3]'
```
If query points to existing property it will be replaced. Note that you can add values only to existing objects/arrays.# License
PHP JSON Pointer is licensed under MIT license.