40

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
  • 45,811
  • 27
  • 120
  • 136
genesis
  • 49,367
  • 20
  • 94
  • 122
  • 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

157

json_encode() has a flag JSON_PRETTY_PRINT

echo json_encode($data, JSON_PRETTY_PRINT);
Mārtiņš Briedis
  • 16,985
  • 5
  • 53
  • 74
  • 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
  • 792
  • 8
  • 19
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.

German
  • 405
  • 4
  • 11
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,126
  • 3
  • 17
  • 21
-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,093
  • 12
  • 12