How to Use 'jq' with 'curl'

Updated

Learn how to use jq with curl to read and filter JSON from an API

jq: How to Use jq with curl

Before we start, make sure you have jq installed on your computer. On a Mac, you can install it with Homebrew:

brew install jq

curl comes with macOS, so there's nothing else to install. First, save the API address in a variable. We'll use a real WordPress blog, which serves its posts at /wp-json/wp/v2/posts:

URL="https://techcrunch.com/wp-json/wp/v2/posts?per_page=10"

Then pipe the response into jq:

curl -s "$URL" | jq .

The -s flag keeps curl silent, so its progress bar doesn't get mixed into the output. The full response is long, so pick out only the fields you need with .[] | {title: .title.rendered, link, date}.

For plain text you can pass to other commands, add -r:

curl -s "$URL" | jq -r '.[].title.rendered'

To filter by a keyword, add select(test("AI")). Be careful with the "i" flag for short words: test("AI"; "i") also matches the letters inside words like said.

undefined

If you'd rather use a developer blog, DEV has a clean public API too: curl -s "https://dev.to/api/articles?per_page=5" | jq -r '.[].title'.