Working with JSON data is a common task for developers, especially when dealing with APIs and configuration files. Often, JSON data comes in a single,Minified line, making it incredibly difficult to read and understand. This is where Pretty Printing Json becomes essential. Fortunately, there are simple and effective ways to format JSON for readability right from your command line. This guide will show you how to use jq
, a powerful command-line JSON processor, to easily pretty print your JSON data.
Let’s say you have a JSON file, like the posts.json
example below, which is currently in a compact, unreadable format:
[{"title":"Open Standards for APIs","link":"https://lornajane.net/posts/2024/open-standards-for-apis","date":"2024-10-09"},{"title":"Lint APIs with Redocly CLI","link":"https://lornajane.net/posts/2024/lint-apis-with-redocly-cli","date":"2024-08-05"},{"title":"API Description Pipelines","link":"https://lornajane.net/posts/2024/api-description-pipelines","date":"2024-06-18"},{"title":"Checking Links in Docs-As-Code Projects","link":"https://lornajane.net/posts/2024/checking-links-in-docs-as-code-projects","date":"2024-04-27"}]
This single-line JSON is hard to parse visually. To make it human-readable, we can use jq
to pretty print JSON.
Using jq
to Pretty Print JSON
jq
is a versatile command-line tool designed for processing JSON data. It allows you to slice, filter, map, and transform JSON with ease. For our purpose of pretty printing JSON, the command is remarkably simple:
cat posts.json | jq "."
Let’s break down this command step-by-step:
cat posts.json
: Thecat
command is a standard Unix utility that reads a file and prints its content to standard output (stdout). In this case, it reads the content ofposts.json
and sends it down the pipeline.|
: This is the pipe operator. It takes the output from the command on the left (cat posts.json
) and directs it as input to the command on the right (jq "."
).jq "."
: This is where the magic happens.jq
is invoked, and"."
is a basic filter injq
that essentially selects the entire JSON input. Crucially, by default,jq
pretty prints its output to stdout.
Running this command will output the following pretty printed JSON directly in your terminal:
[
{
"title": "Open Standards for APIs",
"link": "https://lornajane.net/posts/2024/open-standards-for-apis",
"date": "2024-10-09"
},
{
"title": "Lint APIs with Redocly CLI",
"link": "https://lornajane.net/posts/2024/lint-apis-with-redocly-cli",
"date": "2024-08-05"
},
{
"title": "API Description Pipelines",
"link": "https://lornajane.net/posts/2024/api-description-pipelines",
"date": "2024-06-18"
},
{
"title": "Checking Links in Docs-As-Code Projects",
"link": "https://lornajane.net/posts/2024/checking-links-in-docs-as-code-projects",
"date": "2024-04-27"
}
]
See the difference? The pretty printed JSON is now formatted with indentation and newlines, making it significantly easier to read and understand the structure of the JSON data.
Saving Pretty Printed JSON to a File
While viewing the pretty printed JSON in the terminal is helpful, you might want to save the formatted output to a new file. To do this, you can redirect the output of the jq
command using the >
operator:
cat posts.json | jq "." > better.json
In this command, >
redirects the standard output from jq "."
to a file named better.json
. After running this command, you will have a new file, better.json
, containing the pretty printed JSON content.
Conclusion
Pretty printing JSON is a simple yet incredibly valuable technique for anyone working with JSON data. Using jq
on the command line provides a fast and efficient way to transform unreadable, minified JSON into a well-formatted, human-readable format. Whether you are debugging APIs, examining configuration files, or just trying to understand complex JSON structures, jq
is an indispensable tool in your developer toolkit. Take advantage of this simple command to improve your JSON readability and workflow today!