Reputation: 1115
I am using jq 1.5 and trying to pass two environment variables via jq to create a json object:
export REGIONS="region1,region2"
export KMS_KEYS="key1,key2"
test.json
{
"builders": [
{
"name": "aws"
}
]
}
using the following command:
jq --arg regions $REGIONS --arg kmskeys $KMS_KEYS '.builders[].region_kms_key_ids={$regions}' test.json
current outcome:
{
"builders": [
{
"name": "aws",
"region_kms_key_ids": {
"regions": "region1,region2"
}
}
]
}
desired outcome:
{
"builders": [
{
"name": "aws",
"region_kms_key_ids": {
"region1": "key1",
"region2": "key2"
}
}
]
}
I'm stuck on how to use my REGIONS variable content as the keys and KMS_KEYS variable as the values. Any advice would be appreciated
Upvotes: 0
Views: 1805
Reputation: 50805
Split $regions
and $kmskeys
by commas into two separate arrays, transpose them to pair each region with corresponding kms key and make objects out of them, then add them together to form a single object. E.g:
.builders[].region_kms_key_ids = (
[ $regions/",", $kmskeys/"," ] | transpose | map({(.[0]):.[1]}) | add
)
Upvotes: 2