Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Guide to Linux jq Command for JSON Processing
JSON (JavaScript Object Notation) is a popular data format used for exchanging information between applications. It is lightweight and easy to understand. To process JSON data efficiently, Linux provides a command-line tool called jq. This powerful tool enables users to extract, manipulate, and transform JSON data with ease. With jq, users can quickly perform filtering, sorting, and selecting specific fields. Additionally, jq supports various output formats, making it easy to integrate with other command-line tools.
Installation
Before using jq, ensure it is installed on your system. jq can be installed using the Linux package manager.
Ubuntu/Debian Installation
sudo apt-get install jq
Verify Installation
jq --version
If jq is installed correctly, this command displays the version number.
Basic JSON Processing
Pretty-Print JSON
The . filter selects the entire input object and formats it nicely:
echo '{"fruit":{"name":"mango","color":"yellow","price":4.20}}' | jq '.'
{
"fruit": {
"name": "mango",
"color": "yellow",
"price": 4.2
}
}
Processing Remote JSON Data
Using curl with jq to fetch and format JSON from APIs:
curl -s http://api.open-notify.org/iss-now.json | jq '.'
{
"message": "success",
"iss_position": {
"latitude": "19.4146",
"longitude": "-3.3218"
},
"timestamp": 1678259912
}
Working with JSON Files
Creating a Sample JSON File
Create a file named fruit.json with the following content:
{
"fruit": {
"name": "mango",
"color": "yellow",
"price": 4.2
}
}
Accessing Object Properties
Chain property names to access nested objects:
jq '.fruit.name' fruit.json
"mango"
Retrieve multiple properties using commas:
jq '.fruit.color,.fruit.price' fruit.json
"yellow" 4.2
Array Processing
Iterating Through Arrays
The .[] operator iterates over each array element:
echo '["Aditya","Adil","Arindom"]' | jq '.[]'
"Aditya" "Adil" "Arindom"
Processing Complex Arrays
Create fruits.json with multiple fruit objects:
[
{
"name": "Mango",
"color": "yellow",
"price": 4.2
},
{
"name": "Grape",
"color": "green",
"price": 2.5
},
{
"name": "Blueberry",
"color": "blue",
"price": 1.25
}
]
Extract all fruit names:
jq '.[].name' fruits.json
"Mango" "Grape" "Blueberry"
Advanced Operations
Array Indexing and Slicing
Access specific array elements by index (starting from 0):
jq '.[1].price' fruits.json
2.5
Slice arrays to get subsets:
echo '[10,12,14,16,18,20,22,24,26,28,30]' | jq '.[4:8]'
[ 18, 20, 22, 24 ]
Finding Min/Max Values
Use min and max functions on arrays:
jq '[.[].price] | min' fruits.json
1.25
Filtering with Select
The select function filters objects based on conditions:
jq '.[] | select(.color=="yellow" and .price>=4.2)' fruits.json
{
"name": "Mango",
"color": "yellow",
"price": 4.2
}
Modifying JSON Structure
Delete keys using the del function:
jq 'del(.fruit.color)' fruit.json
{
"fruit": {
"name": "mango",
"price": 4.2
}
}
Common Use Cases
API Response Processing Extract specific fields from REST API responses
Configuration Management Modify JSON configuration files
Data Analysis Filter and aggregate JSON datasets
Log Processing Parse and analyze JSON-formatted log files
Conclusion
The Linux jq command is an essential tool for processing JSON data on the command line. It provides powerful filtering, transformation, and manipulation capabilities that make working with JSON data efficient and straightforward. From basic pretty-printing to complex filtering operations, jq handles diverse JSON processing requirements effectively.
