Skip to content

Instantly share code, notes, and snippets.

@damc-dev
Last active October 20, 2022 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damc-dev/1c60f68a94311949e03b8582591830d8 to your computer and use it in GitHub Desktop.
Save damc-dev/1c60f68a94311949e03b8582591830d8 to your computer and use it in GitHub Desktop.
Loop over JSON list of objects with jq in bash
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
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