bash - Iterating through JSON array in Shell script

Bash - Iterating through JSON array in Shell script

Iterating through a JSON array in a Bash shell script can be accomplished using tools like jq, which is a command-line JSON processor. jq allows you to parse JSON data, extract specific elements, and iterate through arrays. Here's a step-by-step guide on how to iterate through a JSON array using jq:

Example JSON Array

Let's assume you have a JSON file data.json with the following structure:

{ "users": [ { "id": 1, "name": "John Doe", "age": 30 }, { "id": 2, "name": "Jane Smith", "age": 25 }, { "id": 3, "name": "Michael Johnson", "age": 35 } ] } 

Bash Script Using jq

Here's how you can iterate through the users array in Bash using jq:

#!/bin/bash # JSON file containing the array json_file="data.json" # Check if jq is installed if ! command -v jq &> /dev/null; then echo "jq is not installed. Please install jq (https://stedolan.github.io/jq/) to proceed." exit 1 fi # Iterate through the users array jq -r '.users[] | "ID: \(.id), Name: \(.name), Age: \(.age)"' "$json_file" | while IFS= read -r line; do echo "User Info: $line" done 

Explanation

  1. jq Command:

    • .users[]: Selects each element in the users array.
    • "ID: \(.id), Name: \(.name), Age: \(.age)": Formats the output for each user, displaying id, name, and age fields.
    • -r: Outputs raw strings instead of JSON-escaped strings.
    • "$json_file": Specifies the JSON file to process.
  2. while read Loop:

    • IFS=: Disables leading/trailing whitespace trimming and preserves whitespace in the line read.
    • read -r line: Reads each formatted line output by jq.
    • echo "User Info: $line": Prints each user's information.

Running the Script

  1. Save the script to a file, for example, iterate_json.sh.

  2. Make the script executable:

    chmod +x iterate_json.sh 
  3. Run the script:

    ./iterate_json.sh 

Handling More Complex JSON Structures

  • Nested Arrays or Objects: Use nested jq filters to traverse deeper into JSON structures. For example, '.users[].addresses[].city' accesses cities within nested addresses.
  • Error Handling: Ensure jq is installed (command -v jq) before processing JSON to avoid errors.

Using jq in Pipes

You can also pipe JSON data directly into jq instead of reading from a file:

echo '{"users":[{"id":1,"name":"John Doe","age":30},{"id":2,"name":"Jane Smith","age":25}]}' | jq -r '.users[] | "ID: \(.id), Name: \(.name), Age: \(.age)"' 

Summary

  • jq: Powerful tool for parsing JSON in Bash.
  • Iteration: Use .[] to iterate through JSON arrays and nested structures.
  • Output Formatting: Customize output using jq filters to meet your script's needs.

Adjust the jq filter (jq '.users[]', etc.) to fit your specific JSON structure and data extraction requirements within your Bash script.

Examples

  1. How to parse a JSON array in a Bash script?

    json='[{"name": "John"}, {"name": "Jane"}]' for row in $(echo "${json}" | jq -r '.[] | @base64'); do _jq() { echo ${row} | base64 --decode | jq -r ${1} } echo "Name: $(_jq '.name')" done 

    Description: Iterate through a JSON array stored in a variable (json) using jq to extract and display values (e.g., names) for each object.

  2. Bash script to loop through JSON array elements

    json='[{"id": 1, "name": "Apple"}, {"id": 2, "name": "Banana"}]' for (( i=0; i<$(echo ${json} | jq '. | length'); i++ )); do id=$(echo ${json} | jq -r ".[$i].id") name=$(echo ${json} | jq -r ".[$i].name") echo "Item $((i+1)): ID - $id, Name - $name" done 

    Description: Use jq to loop through a JSON array (json) and extract specific fields (e.g., id and name) for each object.

  3. How to handle nested JSON arrays in a Bash script?

    json='{"people": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]}' for person in $(echo "${json}" | jq -r '.people[] | @base64'); do _jq() { echo ${person} | base64 --decode | jq -r ${1} } echo "Name: $(_jq '.name'), Age: $(_jq '.age')" done 

    Description: Iterate through a JSON object containing a nested array (people) using jq in a Bash script to extract and display nested values.

  4. Bash script to loop through JSON array from a file

    while IFS= read -r line; do id=$(echo ${line} | jq -r '.id') name=$(echo ${line} | jq -r '.name') echo "ID: $id, Name: $name" done < file.json 

    Description: Read and iterate through a JSON array stored in a file (file.json) using jq to extract and display specific fields (e.g., id and name).

  5. How to filter JSON array elements in Bash script?

    json='[{"id": 1, "name": "Apple"}, {"id": 2, "name": "Banana"}]' filtered=$(echo "${json}" | jq '[.[] | select(.id == 1)]') echo "Filtered JSON:" echo "${filtered}" 

    Description: Use jq to filter elements in a JSON array (json) based on a condition (e.g., id equals 1) and display the filtered results.

  6. Bash script to process JSON array elements conditionally

    json='[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]' for person in $(echo "${json}" | jq -r '.[] | select(.age > 28) | @base64'); do _jq() { echo ${person} | base64 --decode | jq -r ${1} } echo "Name: $(_jq '.name'), Age: $(_jq '.age')" done 

    Description: Iterate through a JSON array (json) using jq, filter elements based on a condition (e.g., age greater than 28), and display matching values.

  7. How to handle empty JSON arrays in Bash script?

    json='[]' if [[ $(echo "${json}" | jq '. | length') -eq 0 ]]; then echo "Empty JSON array" else echo "Non-empty JSON array" fi 

    Description: Check if a JSON array (json) is empty using jq in a Bash script and print a message based on the result.

  8. Bash script to count elements in a JSON array

    json='[{"id": 1, "name": "Apple"}, {"id": 2, "name": "Banana"}]' count=$(echo "${json}" | jq '. | length') echo "Number of elements: $count" 

    Description: Use jq to count elements in a JSON array (json) and display the total number of elements.

  9. How to extract all values from a JSON array in Bash script?

    json='[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]' names=$(echo "${json}" | jq -r '.[].name') echo "Names: $names" 

    Description: Extract and concatenate values (e.g., names) from all objects in a JSON array (json) using jq in a Bash script.

  10. Bash script to handle multiline JSON arrays

    json='[ {"name": "John", "age": 30}, {"name": "Jane", "age": 25} ]' for person in $(echo "${json}" | jq -c '.[]'); do name=$(echo "${person}" | jq -r '.name') age=$(echo "${person}" | jq -r '.age') echo "Name: $name, Age: $age" done 

    Description: Process a multiline JSON array (json) in a Bash script, extract specific fields (e.g., name and age) using jq, and display them.


More Tags

rtp chai android-navigation memcached wait netcat triggers static-memory-allocation timing request-promise

More Programming Questions

More Math Calculators

More Tax and Salary Calculators

More Statistics Calculators

More Animal pregnancy Calculators