44

You might say this is duplicate of this question, but original question WASN'T answered there. Important part of question is: programmatically?

Is there any php function? Native or homemade?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
genesis
  • 50,477
  • 20
  • 96
  • 125
  • I wonder if you could tweak this to make it work for JSON: http://beautifyphp.sourceforge.net/docs/ – citizen conn Jul 13 '11 at 00:02
  • `json_decode` followed by output buffering of `var_dump` could work, although not the cleanest solution. – Michael Mior Jul 13 '11 at 00:12
  • 2
    `nl2br(json_encode(json_decode('{"test":[{"my":"json"},{"string":"with"},{"pretty":"print"}]}'), JSON_PRETTY_PRINT))` Not talking about the performance though, do a perf test for your JSON on your own – Fr0zenFyr Sep 21 '16 at 08:43

5 Answers5

164

json_encode() has a flag JSON_PRETTY_PRINT

echo json_encode($data, JSON_PRETTY_PRINT);
Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
  • 4
    For a better styling, you could put your encoded JSON output between *
    * tags. Something like _
     YOUR_JSONDATA 
    _
    – Philipos D. Nov 16 '18 at 12:07
3

I created a non-destructive JSON beautifier that support multiple deep levels.

/**
 * JSON beautifier
 * 
 * @param string    The original JSON string
 * @param   string  Return string
 * @param string    Tab string
 * @return string
 */
function pretty_json($json, $ret= "\n", $ind="\t") {

    $beauty_json = '';
    $quote_state = FALSE;
    $level = 0; 

    $json_length = strlen($json);

    for ($i = 0; $i < $json_length; $i++)
    {                               

        $pre = '';
        $suf = '';

        switch ($json[$i])
        {
            case '"':                               
                $quote_state = !$quote_state;                                                           
                break;

            case '[':                                                           
                $level++;               
                break;

            case ']':
                $level--;                   
                $pre = $ret;
                $pre .= str_repeat($ind, $level);       
                break;

            case '{':

                if ($i - 1 >= 0 && $json[$i - 1] != ',')
                {
                    $pre = $ret;
                    $pre .= str_repeat($ind, $level);                       
                }   

                $level++;   
                $suf = $ret;                                                                                                                        
                $suf .= str_repeat($ind, $level);                                                                                                   
                break;

            case ':':
                $suf = ' ';
                break;

            case ',':

                if (!$quote_state)
                {  
                    $suf = $ret;                                                                                                
                    $suf .= str_repeat($ind, $level);
                }
                break;

            case '}':
                $level--;   

            case ']':
                $pre = $ret;
                $pre .= str_repeat($ind, $level);
                break;

        }

        $beauty_json .= $pre.$json[$i].$suf;

    }

    return $beauty_json;

}   
Juan Lago
  • 960
  • 1
  • 10
  • 20
3

had the same question right now. But as you also i'm having php < 5.4. Zend Framework has Zend_Json::prettyPrint(). Works very well.

dr0bz
  • 403
  • 4
  • 12
2

This simple trick did the job for me, I didn't wanted any additional libraries or functions:

$json = '{"status":"0","status_translated":"Request successful!","data":"1"}';
$json_beautified = str_replace(array("{", "}", '","'), array("{<br />&nbsp;&nbsp;&nbsp;&nbsp;", "<br />}", '",<br />&nbsp;&nbsp;&nbsp;&nbsp;"'), $json);

And the result looks like this:

{
    "status":"0",
    "status_translated":"Request successful!",
    "data":"1"
}

This is only for json code that goes 1 step in depth, I hope it helps.

Edi Budimilic
  • 4,526
  • 3
  • 19
  • 22
-2

For command line usage, you can use the js beautifier. No need to share your data with external sites.

https://github.com/vivekpathak/tools/blob/master/jb/jb

vpathak
  • 1,133
  • 12
  • 12