1. Overview
jq is a powerful command line JSON processor to manipulate structured data. It provides a very simple syntax for transforming and manipulating JSON data. Additionally, it is a lightweight and very fast processing tool as it is written in C language. Hence, it has zero runtime dependencies.
In this tutorial, we’ll use the jq command to print JSON data on a single line. Additionally, we’ll explore the jq command with JSON objects, nested JSON, and JSON arrays.
2. Importance of jq
JSON is a structured data format widely used in modern applications as it is very lightweight. In order to process very large and big JSON, it is very difficult to process and manipulate it using Bash. So, jq provides a very easy and handy application for such problems.
The Linux system provides tools like sed, awk, and grep to process the text. These commands are very useful in loosely structured data, but jq is very useful for structured data. Primarily, we use this manipulation of JSON in scripts, automation, and customized output.
3. Print Values in One Line
In order to process data in Linux, we need to install jq as it is not preinstalled in the Linux distribution. To illustrate, let’s first install the jq package, as it is not preinstalled on Linux systems:
$ sudo apt-get install -y jq
In the above command, we installed jq on the Ubuntu machine using sudo privileges. Let’s create a sample JSON that we can use for all the examples:
{
"name": "test_user",
"age": 30,
"city": "Bucharest",
"title": "Java Tutorial",
"category": "Technology",
"tags": ["content writing", "programming"]
}
In the above sample_data.json, we have 6 attributes: name, age, city, title, category, and tags. We’ll use these attributes to print them on one line. Before we dive into printing the JSON output onto a single line, let’s look at the basic syntax of the jq command:
jq '<filter_exp>' <input_file>
In the above command, filter_exp specifies whether we want to extract or want to perform manipulation on the JSON data.
3.1. Printing Single Value
So far, we have a sample JSON sample_data.json to perform various manipulations using the jq command. Now, let’s look at the command to print a single value:
$ jq -r '.name' sample_data.json
test_user
The above command prints the name attribute of sample_data.json. The -r option of the jq command prints the data in raw format without additional formatting.
3.2. Printing the Nested Value
We can also use the jq command for nested JSON. To illustrate, let’s create JSON with nested attributes:
{
"person": {
"name": "test",
"address": {
"city": "Bucharest",
"zip": "01001"
}
}
}
In the above JSON, the value of the city attribute is in a multi-layer nested structure. Let’s look at the command to print nested values using the jq command:
$ jq -r '.person.address.city' nested_sample_json
Bucharest
In the above output, we can see that the value of “person.address.city”.
3.3. Printing Multiple Values
We can also use the jq command to print multiple values of JSON. It helps developers to print useful JSON values from complicated JSON selectively. Now, let’s look at printing multiple values:
$ jq -r '.name, .age' sample_data.json
test_user
30
In the above output, we can see that both the values of name and age are printed on two different lines. The -r option ensures proper values are printed in plain text without any additional quotations.
3.4. Formatting Custom Output
We can also customize the output by concatenating the multiple values with a custom separator. To demonstrate, let’s look at the command:
$ jq -r '"Name: \(.name), Age: \(.age)"' sample_data.json
Name: test_user, Age: 30
In the above output, we can see comma-separated data on a single line.
4. Extracting the JSON Array
We can also use the jq command for JSON arrays along with the typical JSON object. We can play with the filter option of the jq command to work with the array element. Before performing the processing and manipulation of JSON, let’s create a sample JSON array:
[
{
"name": "test",
"age": 30
},
{
"name": "John",
"age": 25
},
{
"name": "Bob",
"age": 35
}
]
In the above sample_array.json, we can see a JSON array with three JSON objects.
4.1. Extracting a Single Value from All Array Elements
jq is a very versatile and powerful tool to process and manipulate JSON data. Using the jq command with the -r option and an appropriate filter expression, we can easily print the JSON data on a single line. In order to extract a single value from all the objects of a JSON array, we can use the map function of the jq command. To illustrate, let’s look at the command to extract only the name values:
$ jq -r '.[] | .name' sample_array.json
test
John
Bob
In the above output, we can see all the names in sample_array.json. The [] operator iterates over all the elements of the JSON array and | pipe each element to the next operation. The .name is used specifically to extract only the “name” value from all the elements.
4.2. Extracting Multiple Values from All Array Elements
We can also extract multiple values from each element of the JSON array in a customized format. To illustrate, let’s look at the command to extract both “name” and “age” in a single line:
$ jq -r '.[] | "Name: \(.name), Age: \(.age)"' sample_array.json
Name: test, Age: 30
Name: John, Age: 25
Name: Bob, Age: 35
In the above output, we can name and age all the JSON elements of the JSON array.
4.3. Filtering Array Elements Based on a Condition
Using the jq command, we can also filter array elements on the basis of a specific condition. To demonstrate, let’s extract the name of the elements with an age greater than 30:
$ jq -r '.[] | select(.age > 30) | .name' sample_array.json
Bob
In the above output, we can see only Bob as its age is greater than 30.
5. Conclusion
In this article, we explored various ways to print values in a single line using the jq command.
First, we created a sample JSON object and JSON array for processing and manipulation. After that, we manipulated JSON objects to extract some values. Later, we also performed the same manipulation on a nested JSON array. Additionally, we printed the JSON values in a customized format as well.