Master JSON Manipulation in Linux: Installing and Using the jq Command
This guide explains how to install jq on CentOS 8, introduces its command‑line syntax, and provides step‑by‑step examples for organizing JSON output, accessing object properties, and iterating over arrays using jq filters.
Introduction
JSON is a key‑value data format widely used for storing and transmitting information between application layers. The jq command‑line tool allows you to parse, filter, and transform JSON directly in a shell environment.
Installing jq on CentOS 8
Run the following command as root to install jq:
dnf -y install jqBasic jq Syntax
The general forms of the jq command are:
jq [options] [file...]
jq [options] --args [strings...]
jq [options] --jsonargs [JSON_TEXTS...]jq supports various filters such as ., |, ,, and .[] to navigate and format JSON data. Common options include --tab, --stream, --indent n, --unbuffered, and -L for library paths.
Organizing JSON Output
Given a file employee.json containing: {"workers":{"name": "John Brooks","id": "003"}} Displaying the raw file with cat produces unstructured output. Using jq with the . filter formats the JSON nicely:
jq '.' employee.json {
"workers": {
"name": "John Brooks",
"id": "003"
}
}The formatted output is easier to read, especially when consuming data from APIs.
Accessing Object Properties
To retrieve a specific property, use the .field filter. For example, to get the entire workers object:
jq '.workers' employee.json {
"name": "John Brooks",
"id": "003"
}To extract a single attribute, chain the field name:
jq '.workers.name' employee.json "John Brooks"Working with Arrays
If the JSON file contains an array of objects:
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]Use .[] to iterate over each element:
jq '.[]' employee.json {
"name": "John Brooks",
"id": "003"
}
{
"name": "Randy Park",
"id": "053"
}
{
"name": "Todd Gray",
"id": "009"
}To access the second element (index 1):
jq '.[1]' employee.json {
"name": "Randy Park",
"id": "053"
}To retrieve a specific field from each array element, combine the filters:
jq '.[].name' employee.json "John Brooks"
"Randy Park"
"Todd Gray"Conclusion
The jq utility transforms JSON data into a readable format and enables precise extraction of values using concise filter expressions. By mastering its installation, syntax, and common filters, you can efficiently process JSON directly from the Linux command line.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
