我正在使用jq 1.5并尝试通过jq传递两个环境变量以创建json对象:
export REGIONS="region1,region2"
export KMS_KEYS="key1,key2"
test.json
{
"builders": [
{
"name": "aws"
}
]
}
使用以下命令:
jq --arg regions $REGIONS --arg kmskeys $KMS_KEYS '.builders[].region_kms_key_ids={$regions}' test.json
当前结果:
{
"builders": [
{
"name": "aws",
"region_kms_key_ids": {
"regions": "region1,region2"
}
}
]
}
期望的结果:
{
"builders": [
{
"name": "aws",
"region_kms_key_ids": {
"region1": "key1",
"region2": "key2"
}
}
]
}
我一直坚持将REGIONS变量内容用作键,将KMS_KEYS变量用作值。任何建议,将不胜感激
用逗号将$regions
和$kmskeys
分割成两个单独的数组,将它们转置以将每个区域与对应的kms键配对,并从中分离出对象,然后将它们加在一起以形成单个对象。例如:
.builders[].region_kms_key_ids = (
[ $regions/",", $kmskeys/"," ] | transpose | map({(.[0]):.[1]}) | add
)