Skip to content

Instantly share code, notes, and snippets.

@knudmoeller
Last active December 4, 2018 08:56
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 knudmoeller/6a5b80d5501bf2621fc8fa7a88ac3b95 to your computer and use it in GitHub Desktop.
Save knudmoeller/6a5b80d5501bf2621fc8fa7a88ac3b95 to your computer and use it in GitHub Desktop.
Recipes for jq
# zip two arrays (from https://github.com/stedolan/jq/issues/609#issuecomment-61155381)
def input:{"id":["cc-by","cc-zero"], "labels":["Creative Commons Attribution", "Creative Commons Zero"]};
def output:
.id as $ids | .labels as $labels
| reduce range(0; $labels|length) as $i
([]; . + [{ "id": $ids[$i], "label": $labels[$i] }]);
input| output
-> [{"id":"cc-by", "label":"Creative Commons Attribution"}, {"id":"cc-zero", "label":"Creative Commons Zero"}]
# zip two arrays, but use a default if the second array doesn't exist:
def input:{"id":["cc-by","cc-zero"]};
def output:
.id as $ids | (.id // .labels) as $labels
| reduce range(0; $labels|length) as $i
([]; . + [{ "id": $ids[$i], "label": $labels[$i] }]);
input| output
-> [{"id":"cc-by", "label":"cc-by"}, {"id":"cc-zero", "label":"cc-zero"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment