PHP code example of johnstevenson / json-works

1. Go to this page and download the library: Download johnstevenson/json-works 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/ */

    

johnstevenson / json-works example snippets



$document = new JohnStevenson\JsonWorks\Document();

$document->addValue('/path/to/array/-', ['firstName'=> 'Fred', 'lastName' => 'Blogg']);

// prettyPrint
$json = $document->toJson(true);

$person = $document->getValue('/path/to/array/0');

$document->addValue('/path/to/array/0/lastName', 'Bloggs');

$document->moveValue('/path/to/array/0', '/users/-');

$document->tidy();
$json = $document->toJson(true);

$document->deleteValue('/users/0');

$document = new JohnStevenson\JsonWorks\Document();

$document->loadData('path/to/data.json');
$document->loadScheme('path/to/schema.json');

if (!$document->validate()) {
    $error = $document->getError();
}

$document->loadSchema($schema);
$document->addValue('/-', ['firstName'=> 'Fred']);

if (!$document->validate()) {
    $error = $document->getError();
    # "Property: '/0'. Error: is missing