Last Updated: February 25, 2016
·
1.344K
· padawin

Format json output

Usage

some-not-formated-json-string | python -mjson.tool

Exemple
Creation of a json.php script which outputs a JSON string:

<?php
$a = array(
    'foo' => 'bar',
    'boo' => 'far'
);

echo json_encode($a);

Here is the output of the script:

> php test-json.php
{"foo":"bar","boo":"far"}

And with json.tool :

> php test-json.php  | python -mjson.tool
{
    "boo": "far",
    "foo": "bar"
}

2 Responses
Add your response

Or you could just use JSON_PRETTY_PRINT:

$a = array(
    'foo' => 'bar',
    'boo' => 'far'
);

echo json_encode($a, JSON_PRETTY_PRINT);

Which will produce the following output:

{
    "foo": "bar",
    "boo": "far"
}
over 1 year ago ·

Hi, Thanks!

However this is php specific. I used php just as an example.

For example in javascript:

ghislain@debian: /tmp
> cat test.js 
console.log(JSON.stringify([{"name": "John"},{"name":"Peter"},{"name":"Dave"}]));
ghislain@debian: /tmp
> node test.js 
[{"name":"John"},{"name":"Peter"},{"name":"Dave"}]
ghislain@debian: /tmp
> node /tmp/test.js | python -mjson.tool
[
    {
        "name": "John"
    },
    {
        "name": "Peter"
    },
    {
        "name": "Dave"
    }
]

or to use to parse an API response for example.

over 1 year ago ·