Jan 6, 2023 3 min read

Pretty Print JSON in Linux Command Line

Table of Contents

While JSON files are preferred for storing data in human-readable form, it is good enough to give headaches when they are minified.

Of course, it will stay in human-readable form in any case but having a well-formatted file is always easy on the eyes. So let me share what you should expect from this guide first:

How to Pretty Print JSON File in Linux Terminal

And throughout this guide, I'll be using the following unstructured JSON file for relevance:

{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}

Now, let me share various ways of prettify JSON files to read it in a structured, human-readable format.

Method 1: Pretty print JSON files using the jq command

The jq is a command-line JSON processor that allows users to modify JSON files in various ways allowing users various options to re-format their JSON files.

But it requires to be installed first. If you're on a Debian-based distro, you can use this command:

sudo apt install jq

And the most straightforward way of re-formatting your JSON files is appending the filename with .:

jq . File.json
pretty print JSON File in Linux using the jq command

And jq allows users to format their files in 2 ways:

  • --tab will add a tab for each indentation level instead of the default two spaces.
  • --indent number allows users to add spaces (up to 8) for each indentation level.

For example, I'll be adding four spaces for each indentation:

jq . --indent 4 File.json
customize the spacing behaviour while printing pretty JSON files in linux

Method 2: Using json_pp to pretty print JSON files

The json_pp is a Perl module intended to convert the input to different output formats and can also be used to pretty print JSON files.

It needs to be piped with the cat command to work. Let me show you how:

cat File.json | json_pp
pretty print JSON File in Linux using the json_pp command

Method 3: Using Python to pretty print JSON file

Being an integral part of the majority of the system, what not to put Python to accomplish our desired task?

To format JSON files using Python, I'll be using json.tool with the -m option:

python3 -m json.tool File.json
pretty print JSON File in Linux using python

Method 4: Using the json_xs command to pretty print JSON file

This won't show any results in STDIN but will make changes in the file itself. Making it the only method that saves changes directly in this entire guide.

For example, I'll save the changes to a new file named Structured.json:

<File.json json_xs >Structured.json
pretty print JSON File in Linux using the json_xs command

Wrapping Up

This was my take on how you can print pretty JSON files in Linux by multiple methods. And if you want to have complete control over formatting, I'd suggest using the first method.

Similarly, you can also pretty print XML in Linux. There is always a way to do things in Linux.

Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

Join the conversation

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.