BASH
Parse and Extract Data from JSON API Responses with curl and jq
Leverage `curl` to fetch JSON data from a web API and use `jq` to efficiently parse and extract specific fields for command-line scripting and automation.
#!/bin/bash
# Define the API endpoint URL
API_URL="https://jsonplaceholder.typicode.com/posts/1"
echo "Fetching data from: $API_URL"
# Fetch the JSON data using curl
# -s: Silent mode, don't show progress or error messages
# -S: Show error messages even in silent mode
# -H "Accept: application/json": Request JSON response
JSON_RESPONSE=$(curl -sS -H "Accept: application/json" "$API_URL")
# Check if curl command was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from $API_URL" >&2
exit 1
fi
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' command not found. Please install it to parse JSON." >&2
exit 1
fi
echo "
--- Raw JSON Response ---"
echo "$JSON_RESPONSE" | jq '.' # Pretty-print the raw JSON
echo "
--- Extracted Information ---"
# Extract specific fields using jq
USER_ID=$(echo "$JSON_RESPONSE" | jq -r '.userId')
POST_TITLE=$(echo "$JSON_RESPONSE" | jq -r '.title')
POST_BODY=$(echo "$JSON_RESPONSE" | jq -r '.body')
# Print the extracted data
echo "User ID: $USER_ID"
echo "Post Title: $POST_TITLE"
echo "Post Body: $POST_BODY"
# Example of extracting multiple fields into a formatted string
echo "
--- Formatted Output ---"
echo "$JSON_RESPONSE" | jq -r '"Post ID: \(.id), Title: \(.title | ascii_upcase)"'
# Example of filtering an array (if the response was an array of objects)
# API_URL_ARRAY="https://jsonplaceholder.typicode.com/posts"
# JSON_RESPONSE_ARRAY=$(curl -sS -H "Accept: application/json" "$API_URL_ARRAY")
# echo "$JSON_RESPONSE_ARRAY" | jq -r '.[] | select(.userId == 1) | .title'
echo "
JSON parsing complete."
How it works: This snippet demonstrates how to interact with a REST API from the command line. It uses `curl` to send an HTTP GET request and retrieve JSON data. The received JSON is then piped to `jq`, a powerful command-line JSON processor. The script shows how to extract individual fields (`.userId`, `.title`, `.body`) and how to format output using `jq`'s string interpolation. This is incredibly useful for scripting API interactions, debugging, and data extraction without writing higher-level language scripts.