Event-based parser for large JSON collections (consumes small amount of memory). Built on top of JSON Streaming Parser
This package is compliant with PSR-4, PSR-1, and PSR-2. If you notice compliance oversights, please send a patch via pull request.
You will need Composer to install the package
Collection must be an array of objects.
[
{
"id": 78,
"title": "Title",
"dealType": "sale",
"propertyType": "townhouse",
"properties": {
"bedroomsCount": 6,
"parking": "yes"
},
"photos": [
"1.jpg",
"2.jpg"
]
},
{
"id": 729,
"dealType": "rent_long",
"propertyType": "villa"
},
{
"id": 5165,
"dealType": "rent_short",
"propertyType": "villa"
}
]
Function as callback:
function processItem(array $item)
{
is_array($item); //true
print_r($item);
}
$parser = new \JsonCollectionParser\Parser();
$parser->parse('/path/to/file.json', 'processItem');
Closure as callback:
$items = [];
$parser = new \JsonCollectionParser\Parser();
$parser->parse('/path/to/file.json', function (array $item) use (&$items) {
$items[] = $item;
});
Static method as callback:
class ItemProcessor {
public static function process(array $item)
{
is_array($item); //true
print_r($item);
}
}
$parser = new \JsonCollectionParser\Parser();
$parser->parse('/path/to/file.json', ['ItemProcessor', 'process']);
Instance method as callback:
class ItemProcessor {
public function process(array $item)
{
is_array($item); //true
print_r($item);
}
}
$parser = new \JsonCollectionParser\Parser();
$processor = new \ItemProcessor();
$parser->parse('/path/to/file.json', [$processor, 'process']);
Receive items as objects:
function processItem(\stdClass $item)
{
is_array($item); //false
is_object($item); //true
print_r($item);
}
$parser = new \JsonCollectionParser\Parser();
$parser->parseAsObjects('/path/to/file.json', 'processItem');
Pass stream as parser input:
$stream = fopen('/path/to/file.json', 'r');
$parser = new \JsonCollectionParser\Parser();
$parser->parseAsObjects($stream, 'processItem');
.json
- raw JSON format.gz
- GZIP-compressed file (you will needzlib
PHP extension installed)
composer test
This library is released under MIT license.