YouTip LogoYouTip

Linux Comm Jq

[![Image 1: Linux Command Encyclopaedia](#) Linux Command Encyclopaedia](#)\n\n* * *\n\njq is a lightweight and powerful command-line JSON processor. It allows you to efficiently parse, filter, transform, and format JSON data, making it particularly suitable for handling JSON data in shell scripts.\n\n### Main Features of jq\n\n* **Streaming Processing**: Can handle JSON data of any size\n* **Rich Operators**: Provides multiple operators to handle JSON data structures\n* **Formatted Output**: Can beautify JSON output for better readability\n* **Cross-platform**: Can run on Linux, macOS and Windows (via WSL)\n\n* * *\n\n## Install jq\n\nIn most Linux distributions, you can easily install jq through the package manager:\n\n## Example\n\n# Ubuntu/Debian\n\nsudo apt-get install jq\n\n# CentOS/RHEL\n\nsudo yum install jq\n\n# Fedora\n\nsudo dnf install jq\n\n# macOS (using Homebrew)\n\n brew install jq\n\nAfter installation, you can verify the installation was successful by running:\n\njq --version\n\n* * *\n\n## Basic Syntax\n\nThe basic command format for jq is as follows:\n\njq [file...]\n### Common Option Parameters\n\n| Option | Description |\n| --- | --- |\n| `-c` | Compact output (no beautification) |\n| `-r` | Output raw strings (remove JSON quotes) |\n| `-s` | Read entire input stream into an array |\n| `-M` | Disable color output |\n| `--arg` | Define variable |\n| `--slurp` | Read multiple JSON objects into an array |\n\n* * *\n\n## Common Operation Examples\n\n### 1. Basic JSON Parsing\n\nAssume we have a file named `data.json` with the following content:\n\n## Example\n\n{\n\n "name": "John Doe",\n\n "age": 30,\n\n "isActive": true,\n\n "address": {\n\n "street": "123 Main St",\n\n "city": "New York"\n\n },\n\n "hobbies": ["reading", "hiking", "coding"]\n\n }\n\n**Get all content (beautified output)**:\n\njq '.' data.json\n**Get specific field**:\n\n## Example\n\njq '.name' data.json\n\n# Output: "John Doe"\n\n**Get nested field**:\n\n## Example\n\njq '.address.city' data.json\n\n# Output: "New York"\n\n### 2. Array Operations\n\n**Get array elements**:\n\n## Example\n\njq '.hobbies' data.json\n\n# Output: "reading"\n\njq '.hobbies[1:3]' data.json\n\n# Output: ["hiking", "coding"]\n\n**Array length**:\n\n## Example\n\njq '.hobbies | length' data.json\n\n# Output: 3\n\n### 3. Conditional Filtering\n\n**Filter elements that meet the condition**:\n\n## Example\n\n# Assume users.json contains an array of users\n\n jq '.[] | select(.age > 25)' users.json\n\n### 4. Data Transformation\n\n**Create new object**:\n\n## Example\n\njq '{fullName: .name, city: .address.city}' data.json\n\n# Output: {"fullName": "John Doe", "city": "New York"}\n\n**Mathematical operations**:\n\n## Example\n\njq '.age * 2' data.json\n\n# Output: 60\n\n### 5. Processing Multiple Files\n\njq -s '. + .' file1.json file2.json\n\n* * *\n\n## Advanced Usage\n\n### 1. Using Variables\n\njq --arg new_city "Boston" '.address.city = $new_city' data.json\n### 2. Processing API Responses\n\ncurl -s https://api.example.com/users | jq '.[] | {name: .name, email: .email}'\n### 3. Complex Data Transformation\n\n## Example\n\n# Convert array of objects to CSV format\n\n jq -r'["Name", "Age"], (.[] | [.name, .age]) | @csv' users.json\n\n### 4. Error Handling\n\n## Example\n\n# Use try-catch to handle fields that may not exist\n\n jq 'try .unknown_field catch "default value"' data.json\n\n* * *\n\n## Practical Application Scenarios\n\n### 1. Log Analysis\n\n## Example\n\n# Analyze JSON-formatted log files\n\ncat app.log | jq 'select(.level == "error") | {time: .timestamp, message: .msg}'\n\n### 2. Configuration Processing\n\n## Example\n\n# Modify a value in the configuration file\n\n jq '.config.timeout = 30' config.json > temp.json && mv temp.json config.json\n\n### 3. Data Statistics\n\n## Example\n\n# Calculate average age of users\n\n jq '[.[].age] | add / length' users.json\n\n* * *\n\n## Frequently Asked Questions\n\n### 1. How to handle large JSON files?\n\nFor very large JSON files, you can use streaming processing:\n\njq -n 'inputs | select(.value > 100)' hugefile.json\n### 2. How to preserve special characters in JSON?\n\nWhen using the `-r` option, special characters are escaped. If you need raw output, you can:\n\njq -j '.' file.json\n### 3. How to merge multiple JSON files?\n\njq -s 'add' file1.json file2.json\n\n* * *\n\n## Summary\n\njq is a powerful tool for processing JSON data. Through learning this article, you should have mastered:\n\n1. Basic installation and usage methods of jq\n2. Common data query and filtering operations\n3. Advanced data transformation and processing techniques\n4. Usage methods in practical application scenarios\n\nTo further improve your jq skills, you can:\n\n* Practice processing various complex JSON data structures\n* Try integrating jq into your shell scripts\n* Consult the jq official manual to learn more advanced features\n\nRemember, mastering jq can significantly improve your efficiency in handling JSON data in the command line!\n\n* * Linux Command Encyclopaedia](
← Linux Comm Dos2UnixLinux Comm Install β†’