judahnator / json-manipulator
A library for JSON string manipulation
Installs: 15 341
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/judahnator/json-manipulator
Requires
- php: ^8.1
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.4
README
What is this?
The short version is it's a library that allows you to interact with JSON strings in a more natural way, as if they were native PHP variables.
Usage
The main class you will be using is \judahnator\JsonManipulator\Json.
It is set up automatically when using the factory function, \judahnator\JsonManipulator\load_json().
For example:
<?php
use function judahnator\JsonManipulator\load_json;
$jsonString = '{"foo":"bar"}';
load_json($jsonString)['foo'] = 'baz';
echo $jsonString;
// '{"foo":"baz"}'
You may also work with nested properties.
<?php
use function judahnator\JsonManipulator\load_json;
$jsonString = '{"foo":["bar","baz"]}';
$jsonObject = load_json($jsonString);
$jsonObject['foo'][] = 'bong';
echo $jsonString;
// '{"foo":["bar","baz", "bong"]}'
You can also add/remove objects and arrays freely.
<?php
use function judahnator\JsonManipulator\load_json;
$jsonObject = load_json();
$jsonObject['object'] = ['foo' => 'bar'];
$jsonObject['array'] = ['zero', 'one', 'two'];
echo $jsonObject;
// {"object":{"foo":"bar"},"array":["zero","one","two"]}