How to Pretty Print JSON in PHP

On 5/13/2016
This php tutorial discusses about pretty printing json in php.

I admit json format is self-descriptive and easy on human eyes. But have you ever created json data and tried printing or storing it in a file? Well! I'm sure you wouldn't say it human-friendly anymore if you take a look on it. All those cluttered characters and braces without proper indentation is a nightmare to read - to say the least.

A while back I tried for a php script to export mysql to json to backup data and use it for later. When I had a look on the file, what I see is this one...

{"a":"ABC","p":"PQR","X":"XYZ"}

instead of something like this with proper indentation and white-space and all...

{
    "a": "ABC",
    "p": "PQR",
    "X": "XYZ"
}
how-to-pretty-print-json-in-php

It sucks! If you use PHP 5.4 or above, consider yourself fortunate as it offers a great option to pretty print json with indentation. All you have to do is to use this option JSON_PRETTY_PRINT when you encode json.

Here let me show you how to pretty print json in php.

Pretty Print JSON in PHP

Recently I shared a php script with my blog readers to write json to file. That one converts an associative array to json string and save it into a file. Let me take the same example and show you how to pretty print json with php script.

<?php
$arr = Array (
    "0" => Array (
        "id" => "MMZ301",
        "name" => "Michael Bruce",
        "designation" => "System Architect"
    ),
    "1" => Array (
        "id" => "MMZ385",
        "name" => "Jennifer Winters",
        "designation" => "Senior Programmer"
    ),
    "2" => Array (
        "id" => "MMZ593",
        "name" => "Donna Fox",
        "designation" => "Office Manager"
    )
);

//pretty print json
header('Content-type: text/javascript');
echo json_encode(array('data' => $arr), JSON_PRETTY_PRINT);
?>

The above script produces a result similar to this one,

{
    "data": [
        {
            "id": "MMZ301",
            "name": "Michael Bruce",
            "designation": "System Architect"
        },
        {
            "id": "MMZ385",
            "name": "Jennifer Winters",
            "designation": "Senior Programmer"
        },
        {
            "id": "MMZ593",
            "name": "Donna Fox",
            "designation": "Office Manager"
        }
    ]
}

Pretty good! But this will just print json on the browser window. If you want to save json into a file then use these below lines of code instead of echoing.

Pretty Print JSON to File

<?php
// encode json
$json = json_encode(array('data' => $arr), JSON_PRETTY_PRINT);

header('Content-type: text/javascript');

//save json as file
if (file_put_contents("data.json", $json))
    echo "Saved json to file...";
else 
    echo "Oops! Error saving json...";
?>    

That's all about pretty printing json in php. With this simple tweak you will get a json file which is easily readable by humans.

6 comments:

Contact Form

Name

Email *

Message *