jq Command Guide: From Installation to Advanced Usage, A Complete Guide to JSON Data Processing

Recently, while working with scripts to process JSON data on Linux systems, I came across the jq command. On Linux systems, jq is a lightweight command-line tool for handling JSON, commonly used for parsing, querying, and manipulating JSON data.

Installing jq

Linux (Ubuntu/Debian)

1
2
sudo apt update
sudo apt install jq

Linux (CentOS/RHEL/Fedora)

Install using the yum or dnf package manager:

1
2
3
4
5
# CentOS/RHEL
sudo yum install jq

# Fedora
sudo dnf install jq

jq Command Usage

To make things easier, I randomly found a piece of JSON data online and saved it as demo.json. Here’s the original content:

1
2
cat demo.json
{"squadName":"Super hero squad","homeTown":"Metro City","formed":2016,"secretBase":"Super tower","active":true,"members":[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]},{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]},{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]}

jq Format Output

The jq command is often used with pipes. For example, the content of demo.json is compressed and hard to read. By using the jq command, we can format the data to make it more readable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
cat demo.json | jq .
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}

jq Get Object Value, Specific Usage .key

1
2
cat demo.json | jq .homeTown
"Metro City"

jq Operations on Arrays in Data

You can perform operations on data arrays using the pipe symbol. Here’s how you can access the second member from the members array.

1
2
3
4
5
6
7
8
9
10
11
cat demo.json | jq '.members | .[1]'
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
}

jq Command Guide: From Installation to Advanced Usage, A Complete Guide to JSON Data Processing

https://www.avayuan.com/jq/

Author

avayuan

Posted on

2024-12-25

Updated on

2025-01-20

Licensed under