Pretty Print JSON responses from `curl` - Part 2
After posting my last TIL , Vinicius showed me another tool that goes beyond just pretty printing: jq
If you don't pass any args to jq
it will just pretty print same as json_pp
:
> curl 'https://til.hashrocket.com/api/developer_posts.json?username=gabrielreis' | jq
{
"data": {
"posts": [
{
"title": "Pretty Print JSON responses from `curl`",
"slug": "pgyjvtuwba"
},
{
"title": "Display line break content in React with just CSS",
"slug": "mmzlajavna"
},
{
"title": "Mutations with the graphql-client Ruby gem",
"slug": "xej7xtsnit"
}
]
}
}
What if you only want to display the first post on the response? Just pass an argument to filter the keys you want. It's like Xpath for JSON: jq '.data.posts[0]'
> curl 'https://til.hashrocket.com/api/developer_posts.json?username=gabrielreis' | jq '.data.posts[0]'
{
"title": "Pretty Print JSON responses from `curl`",
"slug": "pgyjvtuwba"
}
See Part 3
Tweet