Before delving into the process of parsing JSON in a shell script, let's clear up any confusion surrounding the terms "shell" and "Bash."
A shell script is a series of commands written in the shell programming language. The shell is a command-line interface that allows users to interact with an operating system by typing commands. Shell scripts are used to automate tasks, execute commands, and perform various operations using these commands.
Bash is a specific shell type and is one of the most commonly used shells in Unix-like operating systems. It's an enhanced version of the original Unix shell, the Bourne shell (sh). Bash provides additional features, improved usability, and compatibility with various shell scripts.
Now that we have clarified the distinction between shell scripting and Bash let's proceed with parsing JSON in a Bash shell script. JSON, short for JavaScript Object Notation, is an organized data structure commonly seen in modern APIs. It's like a standardized format for sharing information between different systems. Think of it as a translator that humans and machines can easily understand.
Although shell scripting isn't naturally equipped to handle JSON, there's a handy tool called jq
that can make it work. It's like adding a new tool to our toolbox. We need to install jq
using the following command.
sudo apt install jq
jq
Here's a simple breakdown of how we can use jq
to understand and use JSON data in the shell script:
Imagine the JSON file as a well-organized list. We create a file called names.json
with a list of people's names and IDs. It's similar to making a shopping list, but for data:
{"message": "success","number": 10,"people": [{"id": "1","name": "John"},{"id": "2","name": "Jerry"},{"id": "3","name": "Antonio"},{"id": "4","name": "Peter"},{"id": "5","name": "Chris"},{"id": "6","name": "Stewie"}]}
Think of a shell script as a set of instructions for our computer. We write a script named myscript.sh
that tells the computer how to read and understand the JSON data:
#!/bin/shjson=$(cat names.json)number=$(echo "$json" | jq -r '.number')names=$(echo "$json" | jq -r '.people[].name')echo "Number of people: $number"echo "Names: $names"
Before running the script, we need to give the computer permission to run it. We do this by running the following command:
$ sudo chmod +x myscript.sh
Now, let's execute the script:
$ bash myscript.sh
Following is the expected output of the script.
Number of people: 10Names: John Jerry Antonio Peter Chris Stewie
Following is the implementation of the steps describes above:
#!/bin/sh json=$(cat names.json) number=$(echo "$json" | jq -r '.number') names=$(echo "$json" | jq -r '.people[].name' | tr '\n' ' ') echo "Number of people: $number" echo "Names: $names"
Following is the line-by-line breakdown of the code given above:
Line 1: Specify the interpreter for the script, indicating that it's a shell script using the /bin/sh
interpreter.
Line 2: Read the contents of the file names.json
and stores them in a variable named json
.
Line 3: Extracts the value associated with the key "number" from the JSON data stored in the json
variable. The jq
command is used to parse the JSON data. The -r
option outputs the raw value without quotes, storing the result in the number
variable.
Line 4: Extract the "name" key values from each object within the "people" array in the JSON data. The jq
command is again used, and the result is stored in the names
variable. The tr
command is then applied to replace newline characters (\n
) with spaces, effectively joining the names together in a single line.
Line 5: Print the message "Number of people:" followed by the value stored in the number
variable.
Line 6: Prints the message "Names:" followed by the value stored in the names
variable.
Free Resources