PHPackages                             michaelalexeevweb/php-json-chunk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. michaelalexeevweb/php-json-chunk

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

michaelalexeevweb/php-json-chunk
================================

Memory-efficient PHP library for streaming large JSON arrays with chunked reads, generators, and iterators.

1.1.2(2mo ago)16MITPHPPHP &gt;=8.1CI passing

Since Mar 13Pushed 2mo agoCompare

[ Source](https://github.com/michaelalexeevweb/php-json-chunk)[ Packagist](https://packagist.org/packages/michaelalexeevweb/php-json-chunk)[ RSS](/packages/michaelalexeevweb-php-json-chunk/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (9)Dependencies (11)Versions (10)Used By (0)

PhpJsonChunk
============

[](#phpjsonchunk)

[![MIT License](https://camo.githubusercontent.com/eb13d4188ab6c8251cc9bdafcd7f8d8959ad1f70aef00b1a0aa3d92b561e374c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d69636861656c616c65786565767765622f7068702d6a736f6e2d6368756e6b)](LICENSE)[![CI](https://github.com/michaelalexeevweb/php-json-chunk/actions/workflows/ci.yml/badge.svg)](https://github.com/michaelalexeevweb/php-json-chunk/actions/workflows/ci.yml)[![Latest Version](https://camo.githubusercontent.com/5080b91a112177065db68678bd6c1c59f5a8c2a1866ce15631788fd5dfdc9eaa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d69636861656c616c65786565767765622f7068702d6a736f6e2d6368756e6b)](https://packagist.org/packages/michaelalexeevweb/php-json-chunk)[![PHP Version](https://camo.githubusercontent.com/0ffbbe0ffb4cd211378f4b2fd6cf287fa4b0080c6cd68de0cc9ce3de6fc6159f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d69636861656c616c65786565767765622f7068702d6a736f6e2d6368756e6b)](https://packagist.org/packages/michaelalexeevweb/php-json-chunk)[![Total Downloads](https://camo.githubusercontent.com/4a144aba68382fb08db2deaf23ca06f5da34a1ffc1283e465db8a61f865b6886/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d69636861656c616c65786565767765622f7068702d6a736f6e2d6368756e6b)](https://packagist.org/packages/michaelalexeevweb/php-json-chunk)

Memory-efficient **and fast** JSON streaming for large files in PHP. Read large JSON arrays from files in chunks, iterators, or generators without loading the full file into memory — **40% faster than JSON Machine**.

Process large JSON files without running out of memory.

`PhpJsonChunk` is a focused PHP library for streaming **JSON array data** from files. It helps you **stream large JSON files** and **process large JSON datasets** when `file_get_contents() + json_decode()` becomes too expensive for large files.

Why PhpJsonChunk?
-----------------

[](#why-phpjsonchunk)

- ✅ Stream large JSON arrays in PHP
- ✅ Stream large JSON files without loading the full file first
- ✅ Read data item-by-item or chunk-by-chunk
- ✅ Work with nested arrays via `keyPath`
- ✅ Use generators and iterators for memory-friendly processing
- ✅ Apply `limit` and `offset` without loading the full dataset first
- ✅ Optionally spill chunks to temporary files for large workloads

Why not `json_decode()`?
------------------------

[](#why-not-json_decode)

Standard JSON parsing in PHP usually means reading the whole file into memory first and then decoding the whole document. For large JSON files and large datasets, that quickly becomes inefficient or impossible.

`PhpJsonChunk` solves this by streaming JSON array data and returning items or chunks incrementally.

Comparison
----------

[](#comparison)

ApproachMemory usageStreamingSpeed (100k records)`json_decode()`❌ High❌—[`PhpJsonChunk`](https://github.com/michaelalexeevweb/php-json-chunk)✅ **Low**✅**190.3 ms** ⚡[`JsonMachine`](https://github.com/halaxa/json-machine)✅ Low✅330.0 ms[`crocodile2u/json-streamer`](https://packagist.org/packages/crocodile2u/json-streamer)✅ **Minimal**✅383.2 ms[`salsify/json-streaming-parser`](https://github.com/salsify/jsonstreamingparser)✅ Low✅980.2 ms[`MAXakaWIZARD/JsonCollectionParser`](https://github.com/MAXakaWIZARD/JsonCollectionParser)✅ Low✅1025.8 ms[`klkvsk/json-decode-stream`](https://github.com/klkvsk/json-decode-stream)✅ Low✅2585.6 ms> Based on the benchmark below (median of 3 runs), `PhpJsonChunk` is the fastest incremental array reader in this comparison.

Performance
-----------

[](#performance)

Quick snapshot for `100000` records (sorted by speed, faster -&gt; slower):

```
Rank  Parser                     Time         Peak mem
1     PhpJsonChunk               190.3 ms     0.15 MB
2     JsonMachine                330.0 ms     0.31 MB
3     Crocodile2uJsonStreamer    383.2 ms     0.01 MB
4     Salsify                    980.2 ms     0.04 MB
5     JsonCollectionParser      1025.8 ms     0.04 MB
6     JsonDecodeStream          2585.6 ms     0.04 MB

```

Repositories:

- [`PhpJsonChunk`](https://github.com/michaelalexeevweb/php-json-chunk)
- [`JsonMachine`](https://github.com/halaxa/json-machine)
- [`crocodile2u/json-streamer`](https://packagist.org/packages/crocodile2u/json-streamer)
- [`salsify/json-streaming-parser`](https://github.com/salsify/jsonstreamingparser)
- [`MAXakaWIZARD/JsonCollectionParser`](https://github.com/MAXakaWIZARD/JsonCollectionParser)
- [`klkvsk/json-decode-stream`](https://github.com/klkvsk/json-decode-stream)

Full benchmark matrix: [`BENCHMARKS.md`](BENCHMARKS.md)

Notes:

- The other libraries above are compared on the same generated root-array file and iterate items incrementally.

How to reproduce:

```
composer benchmark
```

This runs `bin/benchmark.php` and generates benchmark JSON data on the fly.

You can also run with custom parameters:

```
php bin/benchmark.php --runs=5 --sizes=10000,50000,100000
```

> Benchmark results depend on hardware, PHP version, and OS. Prefer median values from multiple runs.

Install
-------

[](#install)

**Requirements:** PHP 8.1+

```
composer require michaelalexeevweb/php-json-chunk:^1.1.2
```

Quick start
-----------

[](#quick-start)

Stream a large JSON array in chunks:

```
