PHP: json_encode Fun

Last week I needed to convert a large number of “json” files and took on php for that (why not?).

The php 5.4.0 json_encode function features quite a few nice and helpful options like JSON_PRETTY_PRINT. This adds whitespace to the return data to make it easier to read.

This basically makes a lot of custom php functions that I have used in the past obsolete.

Another nice option is JSON_UNESCAPED_SLASHES, which suppresses the Slash escaping that happened with some files. The predefined Constants are well documented on php.net and just waiting to be discovered!

To get serveral options together check out the snippet beneath:

$output_json = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

2 thoughts on “PHP: json_encode Fun

Comments are closed.