From 13bd115958c40a7fdeebb02bf9c7e02d206d3f66 Mon Sep 17 00:00:00 2001 From: medusa Date: Wed, 25 Jun 2025 00:54:08 +0000 Subject: [PATCH] Update tech_docs/linux/jq.md --- tech_docs/linux/jq.md | 112 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/tech_docs/linux/jq.md b/tech_docs/linux/jq.md index de7839e..b6de529 100644 --- a/tech_docs/linux/jq.md +++ b/tech_docs/linux/jq.md @@ -243,4 +243,114 @@ Feel free to modify these examples and try different commands. `jq` has a compre man jq ``` -Happy learning! If you have any specific questions or need further assistance with `jq`, let me know! \ No newline at end of file +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 \ No newline at end of file