Last active
December 4, 2018 08:56
-
-
Save knudmoeller/6a5b80d5501bf2621fc8fa7a88ac3b95 to your computer and use it in GitHub Desktop.
Recipes for jq
This file contains 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
# 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