Linux jq Command
Complete Linux Command Reference
jq is a lightweight and powerful command-line JSON processor. It allows you to parse, filter, transform, and format JSON data efficiently, and is especially suitable for processing JSON data in shell scripts.
Main Features of jq
- Streaming processing: can handle JSON data of any size
- Rich operators: provides a variety of operators to handle JSON data structures
- Formatted output: can beautify JSON output for better readability
- Cross-platform: runs on Linux, macOS, and Windows (via WSL)
Installing jq
On most Linux distributions, you can easily install jq via the package manager:
Examples
sudo apt-get install jq
# CentOS/RHEL
sudo yum install jq
# Fedora
sudo dnf install jq
# macOS (using Homebrew)
brew install jq
After installation, you can verify whether the installation was successful with the following command:
jq --version
Basic Syntax
The basic command format of jq is as follows:
jq [options] [file...]
Common Options and Parameters
| Option | Description |
|---|---|
-c |
Compact output (not beautified) |
-r |
Output raw string (remove JSON quotes) |
-s |
Read the entire input stream into an array |
-M |
Disable color output |
--arg |
Define a variable |
--slurp |
Read multiple JSON objects into an array |
Common Operation Examples
1. Basic JSON Parsing
Suppose we have a file nameddata.jsonwith the following content:
Example
"name": "John Doe",
"age": 30,
"isActive": true,
"address": {
"street": "123 Main St",
"city": "New York"
},
"hobbies": ["reading", "hiking", "coding"]
}
Get all content (beautified output):
jq '.' data.json
Get a specific field:
Example
# Output: "John Doe"
Get a nested field:
Example
# Output: "New York"
2. Array Operations
Get array elements:
Example
# Output: "reading"
jq '.hobbies[1:3]' data.json
# Output: ["hiking", "coding"]
Array length:
Example
# Output: 3
3. Conditional Filtering
Filter elements that meet the condition:
Example
jq '.[] | select(.age > 25)' users.json
4. Data Transformation
Create a new object:
Example
# Output: {"fullName": "John Doe", "city": "New York"}
Mathematical operations:
Example
# Output: 60
5. Processing Multiple Files
jq -s '.[0] + .[1]' file1.json file2.json
Advanced Usage
1. Using Variables
jq --arg new_city "Boston" '.address.city = $new_city' data.json
2. Handling API Responses
curl -s https://api.example.com/users | jq '.[] | {name: .name, email: .email}'
3. Complex Data Transformation
Example
jq -r '["Name", "Age"], (.[] | [.name, .age]) | @csv' users.json
4. Error Handling
Example
jq 'try .unknown_field catch "default value"' data.json
Real-world Application Scenarios
1. Log Analysis
Example
cat app.log | jq 'select(.level == "error") | {time: .timestamp, message: .msg}'
2. Configuration Processing
Example
jq '.config.timeout = 30' config.json > temp.json && mv temp.json config.json
3. Data Statistics
Example
jq '[.[].age] | add / length' users.json
FAQ
1. How to handle large JSON files?
For very large JSON files, you can use streaming processing:
jq -n 'inputs | select(.value > 100)' hugefile.json
2. How to preserve special characters in JSON?
When using the-roption, special characters are escaped. If you need to output them as-is, you can:
jq -j '.' file.json
3. How to merge multiple JSON files?
jq -s 'add' file1.json file2.json
Summary
jq is a powerful tool for processing JSON data. Through this article, you should have mastered:
- Basic installation and usage of jq
- Common data query and filtering operations
- Advanced data transformation and processing techniques
- Usage methods in real-world application scenarios
To further improve your jq skills, you can:
- Practice handling various complex JSON data structures
- Try integrating jq into your shell scripts
- Refer to the official jq manual to learn more advanced features
Remember, mastering jq can significantly improve your efficiency in processing JSON data on the command line!
Other Extensions