Tools: jq Is the Most Underrated Command-Line Tool

Tools: jq Is the Most Underrated Command-Line Tool

JSON Processing Should Not Require Python

Basics

Filter Arrays

API Examples

Transform Data jq processes JSON on the command line. Pipe any API response through it for instant formatting, filtering, and transformation. Install: brew install jq or apt install jq. You will use it every day. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to ? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

# Pretty print -weight: 500;">curl -s https://api.github.com/users/torvalds | jq . # Get one field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .name # "Linus Torvalds" # Get nested field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .company # Pretty print -weight: 500;">curl -s https://api.github.com/users/torvalds | jq . # Get one field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .name # "Linus Torvalds" # Get nested field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .company # Pretty print -weight: 500;">curl -s https://api.github.com/users/torvalds | jq . # Get one field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .name # "Linus Torvalds" # Get nested field -weight: 500;">curl -s https://api.github.com/users/torvalds | jq .company # Get names from array echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[].name' # Filter echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[] | select(.age > 25)' # Map echo '[1,2,3,4,5]' | jq '[.[] * 2]' # [2,4,6,8,10] # Get names from array echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[].name' # Filter echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[] | select(.age > 25)' # Map echo '[1,2,3,4,5]' | jq '[.[] * 2]' # [2,4,6,8,10] # Get names from array echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[].name' # Filter echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq '.[] | select(.age > 25)' # Map echo '[1,2,3,4,5]' | jq '[.[] * 2]' # [2,4,6,8,10] # GitHub: get repo names and stars -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq '.[] | {name, stars: .stargazers_count}' | head -20 # -weight: 500;">npm: get package version -weight: 500;">curl -s https://registry.npmjs.org/express | jq '.["dist-tags"].latest' # Count items -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq length # GitHub: get repo names and stars -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq '.[] | {name, stars: .stargazers_count}' | head -20 # -weight: 500;">npm: get package version -weight: 500;">curl -s https://registry.npmjs.org/express | jq '.["dist-tags"].latest' # Count items -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq length # GitHub: get repo names and stars -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq '.[] | {name, stars: .stargazers_count}' | head -20 # -weight: 500;">npm: get package version -weight: 500;">curl -s https://registry.npmjs.org/express | jq '.["dist-tags"].latest' # Count items -weight: 500;">curl -s "https://api.github.com/users/torvalds/repos" | jq length # CSV from JSON echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq -r '.[] | [.name, .age] | @csv' # Build new objects echo '{"first":"John","last":"Doe","age":30}' | jq '{full_name: (.first + " " + .last), age}' # CSV from JSON echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq -r '.[] | [.name, .age] | @csv' # Build new objects echo '{"first":"John","last":"Doe","age":30}' | jq '{full_name: (.first + " " + .last), age}' # CSV from JSON echo '[{"name":"a","age":20},{"name":"b","age":30}]' | jq -r '.[] | [.name, .age] | @csv' # Build new objects echo '{"first":"John","last":"Doe","age":30}' | jq '{full_name: (.first + " " + .last), age}' -weight: 500;">brew -weight: 500;">install jq -weight: 500;">apt -weight: 500;">install jq