Last active
October 20, 2022 14:18
-
-
Save damc-dev/1c60f68a94311949e03b8582591830d8 to your computer and use it in GitHub Desktop.
Loop over JSON list of objects with jq in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MY_JSON='[{"name": "Banana", "type": "Fruit"},{"name": "Carrot", "type": "Vegetable"},{"name": "Intentionally Blank Type", "type": ""},{"name": "Bread", "type": "Grain"}]' | |
for row in $(jq -r '.[] | @base64' <<< "${MY_JSON}"); do | |
_jq() { | |
base64 --decode <<< "${row}" | jq -r ${1} | |
} | |
NAME=$(_jq '.name') | |
TYPE=$(_jq '.type') | |
echo "${NAME} is a ${TYPE}" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MY_JSON='[{"name": "Banana", "type": "Fruit"},{"name": "Carrot", "type": "Vegetable"},{"name": "Intentionally Blank Type", "type": ""},{"name": "Bread", "type": "Grain"}]' | |
for row_encoded in $(jq -r '.[] | @base64' <<< "${MY_JSON}"); do | |
row="$(base64 --decode<<<"${row_encoded}")" | |
IFS='|' read name type < <(jq -r '[.name, .type] | join("|")'<<<"${row}") | |
echo "${name} is a ${type}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment