PHP memory exhausted ( json_decode) PHP memory exhausted ( json_decode) json json

PHP memory exhausted ( json_decode)


To permanently avoid this, use an event-based JSON parser like https://github.com/salsify/jsonstreamingparser.

That way, the whole thing doesn't have to be in memory at once. Instead, you process the events which give you one piece of the object/array at a time.


There are no other PHP functions that let you decode JSON string. You can try on your own or find library to split JSON into parts.

However you should make sure that it's the only problem. For example before decoding JSON data you maybe created big arrays or create many objects.

If I were you, I would save this json string to file and write extra script just to get it from file and decode to make sure that using json_decode makes the only problem.


One of the simplest ways to iterate big json file in php is to use halaxa/json-machine. You only write one foreach. It will never hit memory limit, because it parses one item a at a time behind the scenes, so the memory consumption is constant no matter the file size.