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.

7
  • ...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 Commented Nov 29, 2012 at 7:08
  • Did you try PHP's json_encode() function? Commented Nov 29, 2012 at 7:08
  • @parker.sikand Yes, I did. As you can see in the PHP code I showed. Commented Nov 29, 2012 at 7:10
  • Why is this not valid JSON for you? Commented Nov 29, 2012 at 7: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... Commented Nov 29, 2012 at 7:12

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thanks a lot! I now have my expected result. I just used this PHP function: 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! :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.