Update tech_docs/linux/jq.md
This commit is contained in:
@@ -244,3 +244,113 @@ man jq
|
||||
```
|
||||
|
||||
Happy learning! If you have any specific questions or need further assistance with `jq`, let me know!
|
||||
|
||||
---
|
||||
|
||||
# Learning `jq` for Command-Line JSON Processing
|
||||
|
||||
`jq` is a powerful command-line JSON processor that lets you parse, filter, and transform JSON data. Here's a comprehensive guide to get you started:
|
||||
|
||||
## Installation
|
||||
|
||||
Most Linux distributions and macOS can install it via package managers:
|
||||
|
||||
```bash
|
||||
# Ubuntu/Debian
|
||||
sudo apt install jq
|
||||
|
||||
# CentOS/RHEL
|
||||
sudo yum install jq
|
||||
|
||||
# macOS (using Homebrew)
|
||||
brew install jq
|
||||
|
||||
# Windows (via Chocolatey)
|
||||
choco install jq
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```bash
|
||||
# Basic pretty-printing
|
||||
jq '.' file.json
|
||||
|
||||
# Read from stdin
|
||||
curl -s https://api.example.com/data | jq '.'
|
||||
```
|
||||
|
||||
## Selecting Data
|
||||
|
||||
```bash
|
||||
# Get a specific field
|
||||
jq '.field' file.json
|
||||
|
||||
# Get nested fields
|
||||
jq '.parent.child.grandchild' file.json
|
||||
|
||||
# Get array elements
|
||||
jq '.array[0]' file.json # First element
|
||||
jq '.array[-1]' file.json # Last element
|
||||
jq '.array[2:5]' file.json # Slice from index 2 to 5
|
||||
```
|
||||
|
||||
## Common Operations
|
||||
|
||||
```bash
|
||||
# Get multiple fields
|
||||
jq '{name: .name, age: .age}' file.json
|
||||
|
||||
# Filter arrays
|
||||
jq '.users[] | select(.age > 30)' file.json
|
||||
|
||||
# Map operations
|
||||
jq '.numbers[] | . * 2' file.json
|
||||
|
||||
# Sort
|
||||
jq '.users | sort_by(.age)' file.json
|
||||
|
||||
# Length/count
|
||||
jq '.array | length' file.json
|
||||
```
|
||||
|
||||
## Advanced Features
|
||||
|
||||
```bash
|
||||
# String interpolation
|
||||
jq '"Hello, \(.name)!"' file.json
|
||||
|
||||
# Conditional logic
|
||||
jq 'if .age > 18 then "Adult" else "Minor" end' file.json
|
||||
|
||||
# Variables
|
||||
jq '. as $item | $item.name' file.json
|
||||
|
||||
# Custom functions
|
||||
jq 'def add(x; y): x + y; add(5; 10)' <<< '{}'
|
||||
```
|
||||
|
||||
## Practical Examples
|
||||
|
||||
```bash
|
||||
# Extract all email addresses from JSON
|
||||
jq -r '.users[].email' file.json
|
||||
|
||||
# Convert JSON to CSV
|
||||
jq -r '.users[] | [.name, .email, .age] | @csv' file.json
|
||||
|
||||
# Sum all values in an array
|
||||
jq '[.numbers[]] | add' file.json
|
||||
|
||||
# Find unique values
|
||||
jq '.tags[]' file.json | sort | uniq
|
||||
|
||||
# Modify JSON structure
|
||||
jq '{user: .name, contact: {email: .email, phone: .tel}}' file.json
|
||||
```
|
||||
|
||||
## Tips
|
||||
|
||||
1. Use `-r` for raw output (no quotes around strings)
|
||||
2. Combine with `curl` for API responses: `curl -s ... | jq ...`
|
||||
3. Use `//` for default values: `jq '.name // "Anonymous"'`
|
||||
4. For large files, use `--stream` for iterative parsing
|
||||
Reference in New Issue
Block a user