Bash – How to Extract Data from JSON File into Variable

bashjsonshell

I have the following json format, basically it is a huge file with several of such entries.

 [
      {
        "id": "kslhe6em",
        "version": "R7.8.0.00_BNK",
        "hostname": "abacus-ap-hf-test-001:8080",
        "status": "RUNNING",
           },
      {
        "id": "2bkaiupm",
        "version": "R7.8.0.00_BNK",
        "hostname": "abacus-ap-hotfix-001:8080",
        "status": "RUNNING",
      },
      {
        "id": "rz5savbi",
        "version": "R7.8.0.00_BNK",
        "hostname": "abacus-ap-hf-test-005:8080",
        "status": "RUNNING",
          },
          
    ]

I wanted to fetch all the hostname values that starts with "abacus-ap-hf-test" and without ":8080" into a variable and then wanted to use those values for further commands over a for loop something like below. But, am bit confused how can I extract such informaion.

HOSTAME="abacus-ap-hf-test-001 abacus-ap-hf-test-005"
for HOSTANAME in $HOSTNAME
do 
  sh ./trigger.sh
done

Best Answer

Parse the JSON with jq:

  • Pull out all the hostname entries.
  • Filter them by the prefix string.
  • Remove the suffix string.
jq -r '.[].hostname | select(startswith("abacus-ap-hf-test")) | rtrimstr(":8080")' file

or, equivalently,

jq -r 'map(select(.hostname|startswith("abacus-ap-hf-test")).hostname | rtrimstr(":8080"))[]' file

For the example document in the question (after removing the nonsensical trailing commas), this generates the following:

abacus-ap-hf-test-001
abacus-ap-hf-test-005

This could be read in a while loop in the shell:

jq -r '.[].hostname | select(startswith("abacus-ap-hf-test")) | rtrimstr(":8080")' file |
while IFS= read -r host; do
    # do something with "$host"
done
Related Question