3

I have this code:

<?php

    header('Content-Type: text/javascript; charset=UTF-8');
    header('Cache-Control: private, no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');

    $data = array(
        "data" => array(
            "sender" => "Jhon Andrew",
            "recipient" => "Someone OverThe Internet",
            "conversation" =>
            array(
                "unix" => "1234567890",
                "message" => "Lorem ipsum dolor sit amet."
            ),
            array(
                "unix" => "0987654321",
                "message" => "Tema tis rolod muspi merol."
            )
        )
    );

    echo json_encode($data);

?>

And I was expecting this kind of result:

{
    "data": {
        "sender":"Jhon Andrew",
        "recipient":"Someone OverThe Internet",
        "message":"Lorem ipsum dolor sit amet."
    }
}

But I got it displayed in just one line, like this:

{"data":{"sender":"Jhon Andrew","recipient":"Someone OverThe Internet","message":"Lorem ipsum dolor sit amet."}}

How can I get properly formatted JSON output just like what I am expecting? It's not really important actually, but I just want to see the result in good format.


...by the way, I just copied the headers from facebooks graph link because that is how I want to output the result. Example: graph.facebook.com/mOngsAng.gA


It is valid of course. All I want to know is how to output it like this: graph.facebook.com/mOngsAng.gA - As you can see it is properly formatted. I mean it has line breaks and indentions. Unlike what I am getting is just showed in one line.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Jhon Andrew
  • 188
  • 3
  • 14
  • ...by the way, I just copied the headers from facebooks graph link because that is how I want to output the result. Example: http://graph.facebook.com/mOngsAng.gA – Jhon Andrew Nov 29 '12 at 07:08
  • Did you try PHP's `json_encode()` function? – parker.sikand Nov 29 '12 at 07:08
  • @parker.sikand Yes, I did. As you can see in the PHP code I showed. – Jhon Andrew Nov 29 '12 at 07:10
  • Why is this not valid JSON for you? – parker.sikand Nov 29 '12 at 07:10
  • I answered too quickly... I am not sure how to format the way you want. But ultimately the formatting should not matter... your result is perfectly valid JSON... – parker.sikand Nov 29 '12 at 07:12
  • It is valid of course. All I want to know is how to output it like this: http://graph.facebook.com/mOngsAng.gA - As you can see it is properly formatted. I mean it has line breaks and indentions. Unlike what I am getting is just showed in one line. – Jhon Andrew Nov 29 '12 at 07:13
  • Possible duplicate of [Pretty-Printing JSON with PHP](http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php) – Don't Panic Apr 13 '17 at 20:13

1 Answers1

4

Take a look at JSON_PRETTY_PRINT flag of json_encode() in php manual. You can simply use:

$data = json_encode($data, JSON_PRETTY_PRINT);

If you aren't using PHP 5.4 or greater try with the accepted answer of the question: Pretty-Printing JSON with PHP.

However, yours is a valid json output!

Community
  • 1
  • 1
jacoz
  • 3,508
  • 5
  • 26
  • 42
  • This is just what I need however I am getting an error, maybe because of my PHP version. I am now reading the link you gave. – Jhon Andrew Nov 29 '12 at 07:29
  • Thanks a lot! I now have my expected result. I just used this PHP function: http://recursive-design.com/blog/2008/03/11/format-json-with-php/ - since I am using PHP version lower than 5.4 | And for people has the same as my problem and using PHP 5.4+ "JSON_PRETTY_PRINT" will do the trick. Thanks again! :] – Jhon Andrew Nov 29 '12 at 07:34