Table of Contents

About

JSON (JavaScript Object Notation) is a widely used data format for exchanging information between web clients and servers. However, JSON data is often densely packed, making it difficult to read when viewing it directly in the terminal. Fortunately, Linux offers several tools to help pretty-print JSON files, making them easier to read and analyze. In this post, we’ll explore various methods for formatting JSON in the Linux terminal, including jq, Python’s built-in JSON tool, and a custom Bash script for environments where installing new tools is not possible.

Using jq

Installation

If jq is not installed on your system, you can usually get it via your package manager:

# On Debian-based systems
sudo apt-get install jq

# On Red Hat-based systems
sudo yum install jq

Usage

jq is a powerful command-line JSON processor. It’s a versatile tool that can be used not only for pretty-printing JSON data but also for parsing, filtering, and transforming it.

Here’s how you can use jq to pretty-print a JSON file:

jq . sample.json

The . in the command represents the identity filter, which outputs the JSON as-is but formatted nicely.

Another, more common, way to use jq is to pipe the results of commands or file output to jq and it will pretty-print the JSON data in the standard output:

echo '{"key1":"value1", "key2":22}' | jq
{
  "key1": "value1",
  "key2": 22
}

Using Python’s Built-in JSON Tool

Installation

If Python is not installed in your Linux distribution, you can usually get it via your package manager:

# On Debian-based systems
sudo apt-get install python3

# On Red Hat-based systems
sudo yum install python3

Usage

Python’s standard library includes a module called json.tool, which can be used as a simple way to pretty-print JSON. This is particularly handy because Python is pre-installed on most Linux distributions. Here’s the command to pretty-print a JSON file using Python:

python3 -m json.tool sample.json

The command reads the JSON file, formats it, and prints it to the standard output.

Similary, we can pipe the results of commands or file output to jq and it will pretty-print the JSON data in the standard output:

echo '{"key1":"value1", "key2":22}' | python3 -m json.tool
{
    "key1": "value1",
    "key2": 22
}

Using custom Bash script

In certain environments, such as systems or Docker containers without internet access, installing new tools may not be feasible. In such cases, a custom Bash script can be used to format JSON files. Here is a simple script that accomplishes this:

#!/bin/bash

function jsonf() {
    file=$1
    indent=0
    while IFS= read -r -n1 char; do
        case $char in
            ("{" | "[") echo "$char"; ((indent+=4)); printf "%${indent}s";;
            ("}" | "]") echo ""; ((indent-=4)); printf "%${indent}s$char";;
            (",") echo "$char"; printf "%${indent}s";;
            (*) printf "%s" "$char";;
        esac
    done < <(sed -e 's/}/}\n/g' -e 's/{/{\n/g' $file)
}

How It Works

  • Input: The script takes a JSON file as input.
  • Indentation: It increases the indentation level after encountering an opening brace { or bracket [, and decreases it after a closing brace } or bracket ].
  • New Lines: It inserts new lines and spaces to align braces and brackets properly.

Installation

We can save the above function to our ~/.bashrc script in order to have the jsonf command available at login. After that we can source the file for our chances to be applied:

source ~/.bashrc

Usage

We can pretty-print a JSON file using the above bash function with:

jsonf sample.json

The function reads the JSON file, formats it, and prints it to the standard output.

Similary, we can pipe the results of commands or file output to jsonf and it will pretty-print the JSON data in the standard output:

echo '{"key1":"value1", "key2":22}' | jsonf
{
    "key1":"value1",
    "key2":22
}