jq cheatsheet
jq is a lightweight and flexible command-line JSON processor, it is also described as sed for JSON data: You can use it to slice and filter and map and transform structured data with the same ease
We’ll use below endpoint as example `https://reqres.in/api/users` which provides response in json format.
Beautify json
curl https://reqres.in/api/users | jq .
Sample output:
2. Print value of top level attribute
curl https://reqres.in/api/users | jq .page
output:
1
3. Print all items in top level array element data
curl 'https://reqres.in/api/users' | jq .data
output:
[
{
"id": 1,
"email": "george.bluth@reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
{
"id": 3,
"email": "emma.wong@reqres.in",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
},
{
"id": 4,
"email": "eve.holt@reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
},
{
"id": 5,
"email": "charles.morris@reqres.in",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://reqres.in/img/faces/5-image.jpg"
},
{
"id": 6,
"email": "tracey.ramos@reqres.in",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://reqres.in/img/faces/6-image.jpg"
}
]
4. Print key, values in array element data
curl 'https://reqres.in/api/users' | jq '.data[] | {first_name,last_name}'
output:
{
"first_name": "George",
"last_name": "Bluth"
}
{
"first_name": "Janet",
"last_name": "Weaver"
}
{
"first_name": "Emma",
"last_name": "Wong"
}
{
"first_name": "Eve",
"last_name": "Holt"
}
{
"first_name": "Charles",
"last_name": "Morris"
}
{
"first_name": "Tracey",
"last_name": "Ramos"
}
5. Print only values from array element data
curl 'https://reqres.in/api/users' | jq '.data[].first_name'
output:
"George"
"Janet"
"Emma"
"Eve"
"Charles"
"Tracey"
6. Print first_name and last_name as text
instead of json
curl 'https://reqres.in/api/users' | jq '.data[] | {first_name,last_name} | join(" ")'
output:
"George Bluth"
"Janet Weaver"
"Emma Wong"
"Eve Holt"
"Charles Morris"
"Tracey Ramos"
7. Get all records for person with first_name
Eve
curl 'https://reqres.in/api/users' | jq '.data[] | select(.first_name == "Eve")'
output:
{
"id": 4,
"email": "eve.holt@reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
}
8. Get email
of a person with first_name
Eve
curl 'https://reqres.in/api/users' | jq '.data[] | select(.first_name == "Eve") | {email}'
output:
{
"email": "eve.holt@reqres.in"
}
9. Extract all keys
from json
curl 'https://reqres.in/api/users' | jq keys
output:
[
"data",
"page",
"per_page",
"support",
"total",
"total_pages"
]
10. Print range from array data
curl 'https://reqres.in/api/users' | jq '.data[1:3]'
output:
[
{
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
{
"id": 3,
"email": "emma.wong@reqres.in",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
}
]
11. Sort array data
by first_name
curl 'https://reqres.in/api/users' | jq '.data | sort_by(.first_name)'
output:
[
{
"id": 5,
"email": "charles.morris@reqres.in",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://reqres.in/img/faces/5-image.jpg"
},
{
"id": 3,
"email": "emma.wong@reqres.in",
"first_name": "Emma",
"last_name": "Wong",
"avatar": "https://reqres.in/img/faces/3-image.jpg"
},
{
"id": 4,
"email": "eve.holt@reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://reqres.in/img/faces/4-image.jpg"
},
{
"id": 1,
"email": "george.bluth@reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
{
"id": 6,
"email": "tracey.ramos@reqres.in",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://reqres.in/img/faces/6-image.jpg"
}
]
12. Count elements in subscripted array data
:
curl 'https://reqres.in/api/users' | jq '.data | length'
output:
6
Try these examples here as well json is pre-loaded.