PHP code example of gemul / jsonparser

1. Go to this page and download the library: Download gemul/jsonparser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gemul / jsonparser example snippets


$json = new \Gemul\JsonParser($json_string);

$json = new \Gemul\JsonParser(storage_path('/app/public/dummy.json'));

$json = new \Gemul\JsonParser($json_string,'->');

$json->getItem('openapi'); // 3.0.3
$json->getItem('info.title'); // Swagger Petstore - OpenAPI 3.0
$json->getItem('info.contact.email'); // [email protected]
$json->getItem('servers.1.url'); // https://petstore3.swagger.io/api/v3

$json->getItem('info.contact'); // stdClass {"email": "[email protected]"}

$json->getItem('info.contact.phone'); // null (path not found in tree, default to null)
$json->getItem('info.contact.phone',1234); // 1234 (set the default to 1234 instead of null)
$json->getItem('info.foo.bar',false); // false (doesn't throw 'Undefined property "foo"' exception)

$json->setItem('info.version','1.0.0');

$json->setItem('info.foo.bar.baz','somevalue');

$json->setItem('servers.2.url', "new url");
//or
$json->setItem('servers.[].url', "new url");

$json->getItem('info.contact.phone.home');
//will return "info.contact", because from 'phone' onward doesn't exist

$json->saveAs(storage_path('/app/public/result.json'));