Last active
April 9, 2025 10:57
-
-
Save kitzberger/6fda7a8ff9c0dda9dbed81cd56cb68fc to your computer and use it in GitHub Desktop.
jq cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This pretty-prints the JSON response: | |
curl -s "https://api.example.com/data" | jq '.' | |
# Useful flags | |
# -c compact | |
# To get all items: | |
curl -s "https://api.example.com/data" | jq '.items[]' | |
# To count all items: | |
curl -s "https://api.example.com/data" | jq '.items[] | length' | |
# To get a specific item: | |
curl -s "https://api.example.com/data" | jq '.items[0]' | |
# If each object in the array contains multiple properties, but you only want specific ones: | |
curl -s "https://api.example.com/data" | jq '.items[] | {id, name}' | |
# In case of of them is a nested property: | |
curl -s 'https://api.example.com/data' | jq '.items[] | { id, name: .attributes.name }' | |
# If you want only items where status is "active": | |
curl -s "https://api.example.com/data" | jq '.items[] | select(.status == "active") | {id, name}' | |
# Get id and name only for active items where price is greater than 100: | |
curl -s "https://api.example.com/data" | jq '.items[] | select(.status == "active" and .price > 100) | {id, name}' | |
# Get id and name only for active items where id belongs to a list | |
curl -s "https://api.example.com/data" | jq '.items[] | select(.id == (123,125,666)) | {id, name}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment