PHP code example of ybushenko / json-parser

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

    

ybushenko / json-parser example snippets



use JsonParser\Config;
use JsonParser\JsonParser;
use JsonParser\Rules\CallableRule;
use JsonParser\Rules\DotPathRule;
use JsonParser\Loader\TextLoader;

$json = <<<JSON
    {
        "data": {
            "general": {
                "id": 12,
                "category": "Eat",
                "url": "/goods/category/eat"
            },
            "goods": [
                {
                    "id":1,
                    "names": {
                        "ru_name":"Печенье",
                        "en_name":"Cookie"
                    },
                    "cost":12.5
                },{
                    "id":2,
                    "names": {
                        "ru_name":"Молоко",
                        "en_name":"Milk"
                    },
                    "cost":17
                },{
                    "id":3,
                    "names": {
                        "ru_name":"Яблоко",
                        "en_name":"Apple"
                    },
                    "cost":19
                }
            ]
        }
    }
JSON;

$rules = [
    'ru_name' => new DotPathRule('names.ru_name'),
    'en_name' => new DotPathRule('names.en_name'),
    'custom' => new CallableRule(function ($item) {
        return $item['names']['ru_name'].$item['names']['en_name'];
    })
];

$config = (new Config($rules))
    ->setLoader(new TextLoader($json))
    ->setBasePath('data.goods')
    ->setIgnoreErrors(true);

$result = (new JsonParser($config))->parse();
var_dump($result);

$loader = new JsonParser\Loader\TextLoader('[{"name": "a", "text": "foo"}, {"name": "b", "text": "bar"}]');

$loader = new JsonParser\Loader\FileLoader(__DIR__ . '/data/goods.json');

$loader = new JsonParser\Loader\UrlLoader('http://url');

$rule = new \JsonParser\Rules\DotPathRule('path.to.node');

$rule = new \JsonParser\Rules\ArrayRule('path.to.nodes', ['node1', 'node2', 'node3']);

$rule = new \JsonParser\Rules\CallableRule(function ($item) { return $item['node']; });

$parser = new \JsonParser\JsonParser($config);
$parser->addDictionary('name', [1 => 'foo', 2 => 'bar']);

$parser = new \JsonParser\JsonParser($config);
$parser->addDictionary('name', 'path.to.categories');

$rule = new \JsonParser\Rules\FromDictionaryRule('name', 'path.to.node_id');

$rule = new \JsonParser\Rules\FromDictionaryRule('name', 'path.to.node_id', function ($value) {
    return trim($value);
});

$config->setIgnoreErrors(true);

$config->setBasePath('path.to.nodes');

$parser->setJson('[{"name": "a", "text": "foo"}, {"name": "b", "text": "bar"}]');

array(3) {
  [0] => array(3) {
    'ru_name' => string(14) "Печенье"
    'en_name' => string(6) "Cookie"
    'custom' => string(20) "ПеченьеCookie"
  }
  [1] => array(3) {
    'ru_name' => string(12) "Молоко"
    'en_name' => string(4) "Milk"
    'custom' => string(16) "МолокоMilk"
  }
  [2] => array(3) {
    'ru_name' => string(12) "Яблоко"
    'en_name' => string(5) "Apple"
    'custom' => string(17) "ЯблокоApple"
  }
}