> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Pretty Print JSON in Linux Command Line
- URL: https://linuxhandbook.com/pretty-print-json/
- Published: 2022-10-19T04:52:30.000Z
- Updated: 2023-01-06T17:13:45.000Z
- Description: JSON is human-readable markup language but reading it in the terminal might be difficult. Learn to format JSON content in a structured way.
- Author: Sagar Sharma
- Tags: Tips

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](https://linuxhandbook.com/content/images/2022/10/Before-and-after-results-1.png)

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](https://linuxhandbook.com/content/images/2022/10/using-jq-1-1.png)

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](https://linuxhandbook.com/content/images/2022/10/json-4-spaces.png)

## 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](https://linuxhandbook.com/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](https://linuxhandbook.com/content/images/2022/10/json_pp.png)

## 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](https://linuxhandbook.com/content/images/2022/10/using-pyhon-for-pretty-JSON.png)

## 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](https://linuxhandbook.com/content/images/2022/10/json_xs.png)

## 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](https://linuxhandbook.com/pretty-print-xml/). There is always a way to do things in Linux.