PHPでJSON文字列を整形する方法です。
json_encode関数は、2番目の引数にJSON_PRETTY_PRINTを指定すると、返される結果の書式を、スペースを使って整えます。
参考
JSON文字列を用意します。
$json = '{"browsers": {"firefox": {"name": "Firefox","pref_url": "about:config","releases": {"1": {"release_date": "2004-11-09","status": "retired","engine": "Gecko","engine_version": "1.7"}}}}}';
JSON文字列をjson_decode関数でデコードし、json_encode関数でエンコードすると、整形された文字列を取得できます。
json_encode(json_decode($json), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
>>> $json = '{"browsers": {"firefox": {"name": "Firefox","pref_url": "about:config","releases": {"1": {"release_date": "2004-11-09","status": "retired","engine": "Gecko","engine_version": "1.7"}}}}}';
=> "{"browsers": {"firefox": {"name": "Firefox","pref_url": "about:config","releases": {"1": {"release_date": "2004-11-09","status": "retired","engine": "Gecko","engine_version": "1.7"}}}}}"
>>> json_encode(json_decode($json), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
=> """
{\n
"browsers": {\n
"firefox": {\n
"name": "Firefox",\n
"pref_url": "about:config",\n
"releases": {\n
"1": {\n
"release_date": "2004-11-09",\n
"status": "retired",\n
"engine": "Gecko",\n
"engine_version": "1.7"\n
}\n
}\n
}\n
}\n
}
"""
>>>