Question: How do I quickly grab a value from JSON output on the command line?
Try: curl -s https://api.github.com/users/torvalds | jq -r '.name, .public_repos, .followers'
Info: jq is a powerful command-line JSON processor. The -r option outputs strings without quotes, while jq filters can extract, filter, map, and transform JSON data.
Examples:
- $
jq '.users[] | select(.active == true)' data.json# Filter active users - $
kubectl get pods -o json | jq -r '.items[].metadata.name'# Extract Kubernetes pod names - $
jq '.[]' file.json# Iterate over a JSON array
Note: Install jq with sudo apt install jq. Use jq filters to extract exactly the fields you need when working with REST APIs, Kubernetes output, or configuration files. For complex filters, test them against sample JSON before using them in production scripts.
Leave a Reply