Display curl output in readable JSON format in Unix shell script

Cover Image for Display curl output in readable JSON format in Unix shell script
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Formatting curl output to readable JSON format in Unix shell script

So, you're building a Unix shell script and want to display the output of a curl command in a readable JSON format? No worries, I've got you covered! 🚀

The Problem: Unreadable JSON output

By default, when you execute a curl command and redirect the output to a file, the JSON data may appear as a single long line, making it difficult to read and understand. In your case, the output looks something like this:

{"type":"Show","id":"123","title":"name","description":"Funny","channelTitle":"ifood.tv","lastUpdateTimestamp":"2014-04-20T20:34:59","numOfVideos":"15"}

The Solution: Formatting JSON with jq

To achieve the desired readable JSON format, we will use a command-line tool called jq. Jq is a lightweight and flexible JSON processor that enables us to manipulate, filter, and format JSON data effortlessly.

Step 1: Installation

Before we proceed, let's ensure that jq is installed on your system. Open your terminal and type the following command to check if jq is installed:

jq --version

If you see the version number, great! You already have jq installed. Otherwise, you can install it using package managers like apt, homebrew, or by downloading the binary from the official website.

Step 2: Formatting the JSON output

Now that we have jq installed let's dive into the actual formatting process. In your shell script, assuming you have the JSON data stored in a variable called $jsonOutput, you can pipe it to jq using the echo command and the -r flag to produce the formatted output.

Here's an example command to format your JSON output:

echo "$jsonOutput" | jq '.' > formatted_output.json

In the above command, we use the . operator in jq to represent the top-level object. By passing $jsonOutput as input to jq, it formats the JSON with proper indentation, making it human-readable. The formatted output is then stored in a file called formatted_output.json.

Take it further: Pretty-printing JSON in terminal

But what if you don't want to save the formatted output in a file? Suppose you want to directly print the pretty-printed JSON on your terminal. Well, guess what? jq can do that too! 🎉

You can modify the last step of the previous command to achieve this:

echo "$jsonOutput" | jq '.'

Now, when you run your shell script, the formatted JSON output will be displayed directly in your terminal.

Putting it all together

Here's the complete shell script code snippet to format your JSON output using jq:

#!/bin/bash

jsonOutput=$(curl some_api_endpoint)
formattedOutput=$(echo "$jsonOutput" | jq '.')

echo "$formattedOutput"

Save the code snippet into a shell script file, make it executable, and run it. You should see your JSON output beautifully formatted on your screen!

Conclusion and Call-to-Action

That's it! You've learned how to format the output of a curl command into a readable JSON format using jq in Unix shell scripts. Now you can easily understand and work with JSON data in your scripts.

Try implementing this solution in your own script and let me know in the comments below if you faced any issues or if it worked like a charm! Feel free to share any other handy tips or tricks you use for working with JSON in shell scripts. Happy coding! 🎉


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello