How to display JSON string in a human readable format on Linux

Last updated on August 27, 2020 by Dan Nanni

Question: I have JSON-formatted string data. Is there a quick and easy way to display a JSON string in a human readable format, preferably from the command line on Linux?

When you are developing a web application, you may often deal with JSON-formatted input/output. When you are inspecting such JSON-formatted data, you may want to see it in a human-readable format (e.g., with proper indentation and line breaks).

In this case, you can use Python's JSON tool. This tool is used to validate and pretty-print any JSON-formatted text. It can be invoked with Python interpreter with -m option.

For example:

$ JSON_STRING='{"name":"Google","location":{"street":"1600 Amphitheatre Parkway","city":"Mountain View","state":"California","country": "US"},"employees":[{"name":"Michael","division":"Engineering"},{"name":"Laura","division":"HR"},{"name":"Elise","division":"Marketing"}]}'
$ echo $JSON_STRING | python -mjson.tool
{
    "employees": [
        {
            "division": "Engineering",
            "name": "Michael"
        },
        {
            "division": "HR",
            "name": "Laura"
        },
        {
            "division": "Marketing",
            "name": "Elise"
        }
    ],
    "location": {
        "city": "Mountain View",
        "country": "US",
        "state": "California",
        "street": "1600 Amphitheatre Parkway"
    },
    "name": "Google"
}

If you are looking to extract individual elements from a JSON-formatted string, you may want to use a more powerful JSON parsing tool such as jq.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean