将值分配给来自 jq 的数组并使用 cb 命令迭代这些值

Assigning the values to array from jq and iterate over the values using cb commands

我在文件中有以下 json,我想从中获取所有价格对象的“ids”并放入数组变量中。

{
  "documentType": "Prices",
  "fullCharges": [
    {
      "ResourceId": null,
      "price": {
        "href": null,
        "id": "8ddaaabc92bc"
      },
      "product": {
        "href": null,
        "id": "123"
      }
    },
    {
      "price": {
        "href": null,
        "id": "326f0f273258"
      },
      "product": {
        "href": null,
        "id": "123"
      }
    }
  ],
  "createdBy": "test",
  "createdOn": "2021-10-05T00:00:55Z",
  "currentSeqNum": 2
}

我正在使用以下查询,但结果以正确的方式出现。

priceIds=$(jq -r .fullCharges[].price.id ${file})

使用上面的命令,它表现得像一个单一的值,而不像数组。如果我打印 priceId 值,它只显示最后一个值。

326f0f273258 instead of 8ddaaabc92bc 326f0f273258

当我遍历它时,它再次表现为单个值。

for price in "${priceIds[@]}"
do
  printf "$price"
  
  cbq -u Administrator -p Administrator  -e "http://localhost:8093"  --script="select * FROM \`com.src.test.price\` where docId==\"$price\";"
  
done

以上循环的输出命令:select * FROM `com.src.test.price` where documentId=="8ddaaabc92bc 326f0f273258";

应该有 2 个这样的命令

select * FROM `com.src.test.price` where documentId=="8ddaaabc92bc"
select * FROM `com.src.test.price` where documentId=="326f0f273258"
  1. 使用额外的括号,它帮助我创建了数组。
  2. 对数组项变量使用下面的 sed 命令,我能够从中删除多余的行。

price=$(echo "$price" | sed 's/^[ \n]*//;s/[ \n]*$//')