Skip to main content

Using jq

There are time when you might want to work with JSON data in bash. While bash does not have a built-in JSON parser, you can use the jq command line utility.

Here is an example script that uses jq to parse JSON data to get values that are used as variables in the script.

export DNS_INFO='{"result":{"id":"a4add2d68160beddd807997c806caf62","zone_id":"e0cc1e6e565d444cfb89bd1e33e4a267","zone_name":"dabble.run","name":"test4.dabble.run","type":"A","content":"170.187.144.196","proxiable":true,"proxied":false,"ttl":3600,"locked":false,"meta":{"auto_added":false,"managed_by_apps":false,"managed_by_argo_tunnel":false,"source":"primary"},"created_on":"2022-01-21T20:53:29.146334Z","modified_on":"2022-01-21T20:53:29.146334Z"},"root@localhost:~# ors":[],"messages":[]}'


export DATE_CREATED=$(echo $DNS_INFO | jq -r .result.created_on)
export IP_ADDRESS=$(echo $DNS_INFO | jq -r .result.content)
export DNS_NAME=$(echo $DNS_INFO | jq -r .result.name)

export README=$(cat <<EOF
*** Managing your Pocket Node: $DNS_NAME ***

Your Pocket Node is now setup. You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=$DNS_NAME

The following details were used:

- DATE CREATED: $DATE_CREATED
- DNS NAME: $DNS_NAME
- IP ADDRESS: $IP_ADDRESS
EOF
)

envsubst <<< "$README" > "README.txt"