How to Install jq on Ubuntu
JSON (JavaScript Object Notation) is the backbone of data interchange in today’s digital landscape. Whether you’re a developer, a system administrator, or simply working with data, having a powerful tool to manipulate JSON data efficiently can save you time and effort. jq is just that tool! In this comprehensive guide, we’ll not only show you how to install jq on your Ubuntu system but also provide practical examples to get you started on your JSON processing journey.
jq allows you to parse, manipulate, filter, and format JSON data efficiently.
We will get the below error if we run jq before the installation, in this guide we will resolve this by installing jq tool
root@cloudscope:~# jq --version
Command ‘jq’ not found, but can be installed with:
snap install jq # version 1.5+dfsg-1, or
apt install jq # version 1.6-1ubuntu0.20.04.1
See ‘snap info jq’ for additional versions.
1. Login to the Ubuntu server and become a root user
sudo su
2. Update the server
Before installing any new software, it’s a good practice to update your package lists to ensure you’re getting the latest available updates from the repo.
Run the below command and Press ‘Y’ to start the Update && Upgrade process.
apt update && apt upgrade
3. Install jq from default Ubuntu repository
apt install jq -y
4. Check version
jq --version
root@cloudscope:~# jq --version
jq-1.6
Practice Examples
Parsing JSON
Let’s start with a basic example of parsing JSON data. Suppose you have a JSON file called employee.json
with the following content:
{
"name": "John",
"age": 30,
"city": "New York"
}
You can use jq to extract the “name” field like this:
jq '.name' data.json
This command will output:
“John”
Formatting JSON
Suppose we have a file called product.json and it has data stored in the below format which is difficult to read
‘[{“Price”: 50, “category_name”: “Electronics”, “description”: “TV”}, {“Price”: 200, “category_name”: “Clothing”, “description”: “Cotton.”},
Below command will pretty-print the the above JSON file, making it easier to read and understand.
jq '.' product.json
[
{
"Price": 50,
"category_name": "Electronics",
"description": "TV"
},
{
"Price": 200,
"category_name": "Clothing",
"description": "Cotton."
},
{
"Price": 300,
"category_name": "Home and Garden",
"description": "Plant."
}
]
Conclusion
This guide has given you a solid foundation to start harnessing the full potential of jq. As you become more familiar with it, you’ll discover its versatility in working with JSON data, making your development and data manipulation tasks smoother than ever.
Now you have the tools you need to master JSON processing on Ubuntu. Happy JSON processing!