Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active December 4, 2017 14:07
Show Gist options
  • Save brigand/3420ff567a597c283b3c7f036abd4030 to your computer and use it in GitHub Desktop.
Save brigand/3420ff567a597c283b3c7f036abd4030 to your computer and use it in GitHub Desktop.
# parse json into global variables
# e.g. s_parse_json prefix <<<'{"x": 1, "y": {"z": 2}}'
# creates prefix_x and prefix_y_z
# doesn't create prefix_y
#
# also arrays:
# e.g. s_parse_json prefix <<<'{"x": ["y", "z"]}'
# creates prefix_x_length prefix_x_0 and prefix_x_1
function s_parse_json () {
local prefix="${1:-body}"
local json="${2}"
if [[ $json = '' ]]; then
json=$(cat)
fi
if [[ $json = '' ]]; then
printf '%s\n' 's_parse_json expected json as either stdin or a second argument' >&2
return 13
fi
local keys=()
while IFS= read -r key; do
keys+=("$key")
done < <(jq 'keys[]' -r <<<"$json")
local key
local is_array=false
if [[ "$(jq 'type' -r <<<"$json")" == "array" ]]; then
is_array=true
fi
for key in "${keys[@]}"; do
local quoted
quoted=$(printf '"%s"' "$key")
if [[ "$is_array" == "true" ]]; then
quoted="[$key]"
fi
local selfquery
selfquery=".${quoted}"
local type
type="$(jq "$selfquery | type" -r <<<"$json")"
if [[ "$type" == "string" ]] || [[ "$type" == "number" ]] || [[ $type == 'null' ]]; then
local var
var="${prefix}_${key}"
declare -g "$var"="$(jq "$selfquery" -r <<<"$json")"
fi
if [[ $type == 'object' ]]; then
local subkey
subkey="$(printf '%s_%s' "$prefix" "$key")"
s_parse_json "$subkey" <<<"$(jq "$selfquery" <<<"$json")"
fi
if [[ $type == 'array' ]]; then
local subkey
subkey="$(printf '%s_%s' "$prefix" "$key")"
local length
length="$(jq "$selfquery | length" <<<"$json")"
local lenkey
lenkey="${prefix}_${key}_length"
declare -g "$lenkey"="$length"
s_parse_json "$subkey" <<<"$(jq "$selfquery" <<<"$json")"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment