Search code examples
phpjsonvar-dump

Human Readable JSON: aka Add spaces and breaks to json dump


Is there a "simple" script somewhere that will take a json data and format it to make it more readable?

For example:

// $response is a json encoded string.
var_dump($response);

The above outputs everything on one line. I'd like for it to be indented and spaced to make it easier to read.


Solution

  • json_encode($response, JSON_PRETTY_PRINT);

    It's 2017 and I think this should be the answer for anyone on a modern version of PHP.

    Note that lots of options exist for how to encode the JSON string to your liking. From php.net: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR