afwy0q
Last Updated: February 25, 2016
·
440
· padawin
B4b98290bc8c9874f8d69d03f8d345fd

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"
}
Say Thanks
Respond

2 Responses
Add your response

17849
A7c1c674da67d39722b33610dad1a761

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 ·
17852
B4b98290bc8c9874f8d69d03f8d345fd

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 ·