judahnator/json-manipulator

A library for JSON string manipulation

v4.0.0 2023-12-26 20:05 UTC

This package is auto-updated.

Last update: 2024-10-27 18:22:05 UTC


README

pipeline status coverage report

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"]}