victorap93/readjson

Read Json makes it easy to read and work with json files.

2.0.0 2022-04-02 14:35 UTC

This package is auto-updated.

Last update: 2024-09-09 18:34:41 UTC


README

Software License Latest Version Total Downloads

Read Json makes it easy to read and work with json files.

Installation

The recommended way to install this is through Composer.

composer require victorap93/readjson

How to use

Assuming you have a .json file ./readjson/stores.json with the following content.

{
    "stores": [
        {
            "name": "store one",
            "products": [
                {
                    "name": "product one",
                    "amount": 10
                },
                {
                    "name": "product two",
                    "amount": 20
                }
            ]
        },
        {
            "name": "store two",
            "products": [
                {
                    "name": "product two",
                    "amount": 22
                },
                {
                    "name": "product three",
                    "amount": 33
                }
            ]
        }
    ]
}

And this .php in the same folder ./readjson/index.php, the example below show different ways to acess .json file.

use \victorap93\ReadJson;

$json_path = "./stores.json";

// Get all json content.
$ReadJson = new ReadJson($json_path);
$json_object_value1 = $ReadJson->getJsonObject();

// Accessing the obtained object.
$json_object_value2 = $json_object_value1->stores[0]->products;

// Get a specified json content position in instance of class.
$ReadJson = new ReadJson($json_path, ['stores', 0, 'products']);
$json_object_value3 = $ReadJson->getJsonObject();

// Get a specified json content position in method call.
$ReadJson = new ReadJson($json_path, ['stores', 0]);
$json_object_value4 = $ReadJson->getJsonObject(['products']);

// Get a specified json content position with object.
$json_object_value5 = $ReadJson->accessRecursiveKeys($json_object_value4, [0, 'name']);

Help and docs

License

Read Json is made available under the MIT License (MIT). Please see License File for more information.