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

Question: I have JSON-formatted string. Is there a quick and easy way to display 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 in a JSON-formatted string, you may want to use a more powerful JSON parsing tool such as jq.

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *