jq curl Extract Data from API Responses in Terminal

User avatar placeholder
Written by Tamzid Ahmed

June 1, 2026

When working with APIs from the terminal, developers often need to quickly jq curl extract data from JSON responses. This powerful combination eliminates the need for external scripts or browser-based debugging, turning raw API output into actionable insights in seconds. In this guide, you’ll master practical techniques for parsing API data directly in your terminal environment.

Why jq and curl? The Perfect Terminal Duo for API Work

curl handles HTTP requests with precision, while jq specializes in filtering and transforming JSON data. Together, they form an indispensable toolkit for developers who work with APIs daily. This duo operates entirely in the terminal, making it ideal for scripting, debugging, and rapid prototyping without context switching.

Unlike GUI-based tools, this approach integrates seamlessly into automation workflows and CI/CD pipelines. It’s particularly valuable when working with authentication tokens, pagination, or nested data structures where manual parsing becomes tedious.

Prerequisites: Setting Up Your Terminal Environment

Ensure both tools are installed before proceeding:

  • curl: Pre-installed on macOS, Linux, and Windows (via WSL or Git Bash)
  • jq: Install via package managers:
  • macOS: brew install jq
  • Ubuntu/Debian: sudo apt install jq
  • Windows (Chocolatey): choco install jq

Verify installations with curl --version and jq --version. Most modern systems support these tools out-of-the-box.

Basic jq Syntax: Filtering JSON with Precision

jq uses intuitive path expressions to navigate JSON structures. Key syntax patterns include:

  • Top-level field: jq '.field'
  • Nested field: jq '.parent.child'
  • Array elements: jq '.array[] | .field'
  • Raw output (no quotes): jq -r '.field'

For example, processing {"user":{"name":"Alice"}} with jq '.user.name' returns "Alice", while jq -r '.user.name' outputs Alice without quotes.

Step-by-Step: Extracting Data from an API Response

Let’s use the public reqres.in API for practical examples. First, fetch user data:

curl https://reqres.in/api/users

This returns a JSON array of user objects. Now extract the first user’s email:

curl https://reqres.in/api/users | jq '.data[0].email'

Output: "george.bluth@reqres.in"

Handling Nested Structures and Arrays

For complex responses, chain filters to drill down:

curl https://reqres.in/api/users | jq '.data[] | {id, email, first_name}'

This returns each user’s ID, email, and first name as individual objects. To get all first names in a flat list:

curl https://reqres.in/api/users | jq '.data[].first_name'

Practical Authentication Example: Token Extraction

Many APIs require authentication tokens. Here’s how to extract one from a login response:

curl -X POST https://api.example.com/auth
-d '{"user":"admin","pass":"secret"}'
| jq -r '.access_token'

This outputs the token without quotes for direct use in headers. Combine it with subsequent requests:

TOKEN=$(curl -X POST https://api.example.com/auth -d '{"user":"admin","pass":"secret"}' | jq -r '.access_token')
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/data

Advanced Tips: Error Handling and Optimization

Always validate JSON responses before parsing. Use jq empty to check for valid JSON:

curl -s https://api.example.com/endpoint | jq empty

If the response isn’t valid JSON, jq exits with an error. For missing fields, provide defaults:

jq '.optional_field // "default_value"'

Format large responses for readability with jq ., or use -C for colorized output. For pagination, extract next-page URLs directly: jq '.next_page'.

Conclusion

Mastering jq curl extract data techniques transforms how developers interact with APIs. This terminal-native workflow saves significant time during debugging, scripting, and integration tasks. Start small by extracting a single field from a public API, then gradually tackle more complex structures. Remember: the most powerful tool is the one you use consistently—integrate these commands into your daily terminal habits to boost productivity instantly.

Leave a Comment