BASH
Extract and Manipulate JSON Data with `jq`
Master `jq` for parsing, filtering, and transforming JSON data in Bash, an essential tool for interacting with web APIs and managing JSON configuration files.
#!/bin/bash
# Example JSON data (can also be piped from curl or a file)
JSON_DATA='{
"name": "WebApp Server",
"status": "running",
"uptime_seconds": 86400,
"users": [
{"id": 1, "username": "admin"},
{"id": 2, "username": "dev_user"}
],
"config": {
"port": 8080,
"ssl_enabled": true
}
}'
echo "Original JSON:"
echo "$JSON_DATA" | jq '.'
echo "
--- Extracting Specific Values ---"
# Get the 'status' field
echo "Server Status: $(echo "$JSON_DATA" | jq -r '.status')"
# Get nested 'port' from 'config'
echo "Config Port: $(echo "$JSON_DATA" | jq -r '.config.port')"
# Get username of the first user
echo "First User: $(echo "$JSON_DATA" | jq -r '.users[0].username')"
echo "
--- Filtering and Transforming ---"
# Select only 'name' and 'status'
echo "Name and Status: $(echo "$JSON_DATA" | jq '{name: .name, status: .status}')"
# Filter users to get only usernames as an array
echo "All Usernames: $(echo "$JSON_DATA" | jq '[.users[].username]')"
# Add a new field to the JSON (example: 'version')
echo "JSON with new field: $(echo "$JSON_DATA" | jq '. + { "version": "1.0.0" }')"
# Use jq to format pretty-print from a URL (requires curl)
# echo "
--- Pretty Print from API ---"
# curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=1' | jq '.'
How it works: `jq` is a lightweight and flexible command-line JSON processor. It allows you to slice, filter, map, and transform structured data with ease. The `-r` flag outputs raw strings without quotes. You can navigate JSON objects using `.` (e.g., `.status`, `.config.port`) and arrays using `[]` (e.g., `.users[0]`). `jq` can also transform JSON, like creating new objects (`{key: .value}`), filtering arrays (`[.users[].username]`), or adding/modifying fields (`. + {"new_key": "value"}`). It's an indispensable tool for working with web APIs and configuration files in Bash.