Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Last active September 14, 2022 00:17
Show Gist options
  • Save shollingsworth/45000d43000adab74742e6c083f47800 to your computer and use it in GitHub Desktop.
Save shollingsworth/45000d43000adab74742e6c083f47800 to your computer and use it in GitHub Desktop.
AWS tags - convert a json key value {"foo": "bar", "baz": "qux"} to [{ "Key": "foo", "Value": "bar"}, {"Key": "baz", "qux"}]
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
TAGS_JSON=$(
cat <<EOF
{
"foo": "bar",
"baz": "qux"
}
EOF
)
# convert to Key, Value
TAGS="$(echo "$TAGS_JSON" | jq -r 'to_entries | map("\"Key\":\"\(.key)\",\"Value\":\"\(.value)\"") | join("\n")')"
# convert to array
TAGS=$(echo "${TAGS}" | sed 's/^/\{/;s/$/\},/' | tr -d '\n' | sed 's/,$//')
TAGS="[${TAGS}]"
# output (valid json)
echo "${TAGS}" | jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment