4.0 KiB
Json Data
jq is your lightweight, flexible command-line JSON processor. It's built in portable C with zero runtime dependencies, making it simple to install and powerful for parsing JSON in Bash.
Demo Setup: QuizAPI
This demo uses the QuizAPI to fetch JSON data. Obtain a free API key from their client area to follow along.
Installing jq
jq is easily installed via your system's package manager or direct download.
- Ubuntu/Debian:
sudo apt-get install jq - Red Hat/Fedora:
sudo dnf install jq - Arch Linux:
sudo pacman -S jq
For other systems, refer to the official jq download page. Verify your installation:
jq --version
Basic JSON Parsing with jq
Once jq is installed and you have your QuizAPI key, you can start processing JSON.
First, set your API key:
API_KEY=YOUR_API_KEY_HERE # Replace with your actual key
Fetch questions using curl:
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10"
The raw output is hard to read. Pipe it to jq for beautifully formatted and colored JSON:
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10" | jq
This command makes the JSON output structured and easy to inspect.
Extracting Specific Elements
To get only the first element from a JSON array, use .[0]:
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10" | jq '.[0]'
This will output only the first JSON object in the array.
Fetching Key Values Across an Array
To extract the value of a specific key (e.g., question) from every object in a JSON array, use .[].key:
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10" | jq '.[].question'
This will return a list of all questions from the API response.
Integrating jq into a Bash Script
Let's create a script to fetch a question and its answers, then display them. We'll use jq to parse specific fields and assign them to Bash variables.
Create quiz_script.sh:
touch quiz_script.sh
Open quiz_script.sh and add (remember to replace YOUR_API_KEY_HERE):
#!/usr/bin/env bash
# Your QuizAPI Key (replace YOUR_API_KEY_HERE with your actual key)
API_KEY="YOUR_API_KEY_HERE"
# Make an API call and get the first question object
# Using -s for silent mode to suppress curl's progress meter
QUIZ_DATA=$(curl -s "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=1" 2>/dev/null)
# Extract specific values using jq with raw output (-r) for Bash variables
question=$(echo "${QUIZ_DATA}" | jq -r '.[0].question')
answer_a=$(echo "${QUIZ_DATA}" | jq -r '.[0].answers.answer_a')
answer_b=$(echo "${QUIZ_DATA}" | jq -r '.[0].answers.answer_b')
answer_c=$(echo "${QUIZ_DATA}" | jq -r '.[0].answers.answer_c')
answer_d=$(echo "${QUIZ_DATA}" | jq -r '.[0].answers.answer_d')
# Output the question and answers
echo "
Question: ${question}
A) ${answer_a}
B) ${answer_b}
C) ${answer_c}
D) ${answer_d}
"
Save, make executable, and run:
chmod +x quiz_script.sh
./quiz_script.sh
The script will print a random question and its corresponding answers. For more advanced, interactive Bash quizzes, explore community projects that build upon these jq principles.
Conclusion
jq empowers you to process JSON directly within your Bash terminal, facilitating seamless interaction with REST APIs. For deeper dives, consult the official jq manual and the QuizAPI documentation.
{notice} This content was inspired by a piece by softwareshinobi on Dev Team Six.