PHP json_encode size limit?

To help others who may be running into problems that they can't explain with json_encode. I've found it helps to know about the json error msg function.

json_last_error_msg();

I was having a similar problem but it wasn't related to file size. I had malformed utf-8 in the database. You can check your json like this

$json = json_encode($data);

if ($json)
    echo $json;
else
    echo json_last_error_msg();

PHP docs here json_last_error_msg


PHP 5.3: ext/json/json.c
PHP 7 (current): ext/json/json.c

There is no built-in restriction to the size of JSON serialized data. Not for strings anyway. I would therefore assume you've run into PHPs memory limit or something.

json_encodeing a string consistently just adds some escaping and the outer double quotes. Internally that means a bit memory doubling (temporary string concatenation and utf8_to_utf16 conversion/check), so that I ran into my 32MB php memory limit with an 8MB string already. But other than that, there seem to be no arbitrary limits in json.c

Tags:

Php

Json

Size

Limit