Parsing JSON in Linux/Mac: A Comprehensive Guide
Last Updated on :December 7, 2024
jq is a lightweight and powerful command-line tool that simplifies JSON parsing. It provides a rich set of features for querying, filtering, and transforming JSON data.
Using jq to parse JSON:
Suppose you have a JSON file named data.json:
{
"name": "Rakesh Kumar",
"age": 35,
"city": "Bangalore"
}
You can extract the value of a specific key as follows:
cat data.json | jq '.name'
This will extract and print the values of the name, age, and city keys from the JSON file
cat data/data.json | jq '.name, .age, .city'
If your JSON contains arrays, you can access elements by their index. For example:
data.json
{
"name": "Rakesh Kumar",
"details" : {
"age": 35,
"city": "Bangalore"
},
"friends" : ["John", "Jane", "Alice", "Bob"]
}
To extract the first element of friends array:
cat data.json | jq '.friends[0]'
Nested JSON:
For nested JSON objects, you can access keys using the dot notation.
Suppose data.json is :
{
"name": "Rakesh Kumar",
"details" : {
"age": 35,
"city": "Bangalore"
}
}
You can extract nested values like this:
cat data.json | jq '.details.age'
Using jq to parse API Response:
Parsing an API response with jq involves fetching the data using a tool like curl and then piping the output to jq for processing. Here’s a step-by-step example:
curl -s http://localhost:3000/employees
Response -
[
{
"id": "1",
"name": "John Doe",
"email": "[email protected]",
"department": "Engineering"
},
{
"id": "2",
"name": "Jane Smith",
"email": "[email protected]",
"department": "Marketing"
},
...
Parse and format the response with jq
curl -s http://localhost:3000/employees | jq -r '.[] | "Name: \(.name)\nEmail: \(.email)\nDepartment: \(.department)"'
This will output:
Name: John Doe
Email: [email protected]
Department: Engineering
Name: Jane Smith
Email: [email protected]
Department: Marketing
...
Parsing JSON in a Shell Script:
You can integrate jq commands into a shell script. Below is an example script that reads a JSON file and prints a specific value:
employee_arr.json
{
"employees": [
{
"id": "1",
"name": "John",
"email": "[email protected]",
"department": "Engineering"
},
{
"id": "2",
"name": "Jane",
"email": "[email protected]",
"department": "Marketing"
},
….
]
}
#!/bin/bash
#
# SCRIPT: json_parser.sh
# AUTHOR: Rakesh Panigrahi
# PURPOSE: Script that parses JSON data
# echo "Printing Array Of Employess---------------------------------"
# Parse JSON using jq
employees=$(jq '.employees' data/employee_arr.json)
# Iterate over each employee
for employee in $(echo "${employees}" | jq -c '.[]'); do
name=$(echo "$employee" | jq -r '.name')
email=$(echo "$employee" | jq -r '.email')
department=$(echo "$employee" | jq -r '.department')
echo "Name: $name, Email: $email, Department: $department"
done
By using jq, you can efficiently parse and manipulate JSON data within your shell scripts on a Linux system.