Exploring Efficient JSON Handling Techniques in Linux Shells
Written on
Chapter 1: Introduction to JSON in the Shell
Who doesn’t appreciate a well-structured JSON object? Today, JSON is one of the most common data formats we encounter, whether fetching information from an API or querying a database. Many popular programming languages offer built-in capabilities to parse and manipulate JSON data. However, working with JSON in a Bash shell can be more challenging.
Bash and similar shells do not come with a default JSON parser, which means you either need to switch to a programming language interpreter or install a dedicated utility. If your goal is to remain strictly within the shell environment, adding a specialized program is often the easiest route. This allows you to avoid the hassle of setting up an entire programming language and its dependencies.
The good news is that most JSON manipulation tools are lightweight, fast, and don't require additional libraries. In this article, we will delve into several popular utilities that enhance your shell's ability to handle JSON data seamlessly.
Section 1.1: jq - The Go-To JSON Processor
Without a doubt, jq is one of the most widely used tools for working with JSON. It excels at filtering and parsing JSON data, making it invaluable for shell scripts that interact with APIs. Additionally, jq is compact and incredibly efficient. Here's how you can use jq in conjunction with an API call:
When you inspect the raw output of this API call, you will see a complex JSON structure. For instance, the output might look something like this:
{"results":[{"gender":"female","name":{"title":"Mrs","first":"Alice","last":"Williams"},...}]}
By piping this output into jq, you can perform queries to extract the data you need. If you simply pass the output to jq, it will format it for easier reading. If your goal is to retrieve a random name from the API, you would do the following:
This command would yield a result like this:
{
"title": "Miss",
"first": "Charline",
"last": "Legrand"
}
Now, you have an efficient way to manipulate API responses without leaving the shell.
The capabilities of jq extend beyond basic filtering. For instance, if you wanted to create a new key called fullname that combines both first and last names, you could easily achieve this with jq:
This would return:
{
"fullname": "Charline Legrand"
}
In a manner similar to chaining shell commands, you can also combine jq filters to enhance your data processing.
Section 1.2: Introducing jo for JSON Creation
While jq is excellent for parsing existing JSON data, jo is the perfect tool for constructing JSON structures directly from the shell. This utility simplifies the process, allowing you to create JSON data without the hassle of manual string concatenation or escape sequences. Here's a quick example of how easy it is to use jo:
$ jo name=bob creation_date="$(date +%m-%d-%y)"
This command will produce:
{"name":"bob","creation_date":"02-23-21"}
Instead of manually formatting each element, you can declare items like regular variables. Jo also allows you to capture the output of subshells effortlessly.
Additionally, you can pretty print larger JSON objects using the -p option. For example:
$ jo -p arr=$(jo -a one two three four five six)
This results in:
{
"arr": [
"one",
"two",
"three",
"four",
"five",
"six"
]
}
For more information on jo and its capabilities, you can refer to its documentation.
Section 1.3: json_pp - Pretty Printing JSON
json_pp is a straightforward utility that formats larger JSON structures for better readability and can convert between various formats. When you run:
It will output a neatly formatted JSON structure like this:
{
"info" : {
"page" : 1,
"results" : 1,
"seed" : "83b8f82090d14bfb",
"version" : "1.3"
},
...
}
Although it doesn't have the extensive features of jq or jo, json_pp is useful for quickly making JSON more readable in urgent situations.
Section 1.4: jshon - A Lightweight JSON Parser
Another lightweight option for JSON manipulation is jshon, which offers functionalities similar to jq. It provides quick access to key names in a JSON object. For instance, if you want to retrieve all the keys from an object, you can use:
$ echo '{"key1":"value1","key2":"value2","key3":"value3"}' | jshon -k
This will give you:
key1
key2
key3
While jq returns the keys as an array, jshon simply outputs them as plain text. jshon is remarkably fast, outperforming jq significantly in benchmarks for the keys function.
Thanks for joining us! Processing JSON in shell scripts or directly in the console can be streamlined with these powerful utilities. For further skill enhancement, check out "6 Terminal Commands You Should Know."
Chapter 2: Additional Resources
This video titled "Bash Script: Using jq (command-line JSON parser) to parse and use data from a JSON file" provides a comprehensive overview of jq and its applications.
In this tutorial, "Parsing JSON on the command line | Easy | JQ tutorial with example code," you'll find practical examples and tips for using jq effectively.