S.M_Emamian S.M_Emamian - 1 year ago 118
JSON Question

json viewer library for php

I would like to show my json string like this :

enter image description here

but if I echo my json output it shows like this:


is there any plugin/library to show json string as well as above image (formatted json) ?

Answer Source

When you use json_encode in PHP you can specify the JSON_PRETTY_PRINT option.

echo json_encode(["foo" => ["bar" => ["baz" => "quix"]]], JSON_PRETTY_PRINT);

This gives you output like this.

    "foo": {
        "bar": {
            "baz": "quix"

If what you already have is a string you can just decode first and the encode.

$str = '{"Status":2,"TokenReg":"eeea7930efeb7715697a2035fcee3fdf","AllScores":"305","User_ID":"16433"}';
echo json_encode(json_decode($str), JSON_PRETTY_PRINT);

This should give you the desired output.

    "Status": 2,
    "TokenReg": "eeea7930efeb7715697a2035fcee3fdf",
    "AllScores": "305",
    "User_ID": "16433"