小编ext*_*int的帖子

JQ 在 Bash 数组上循环添加元素

我似乎无法找到答案,但已经看到足够多的东西,知道可能有更好的方法来做我想做的事情。

问题:我有一个 bash 数组。对于 bash 数组中的每个元素,我想更新一个 JSON 数组。

JSON 如下所示。我想更新水果数组。

  "foods": {
    "perishable": {
      "fruit": []
Run Code Online (Sandbox Code Playgroud)

我将得到一个长度为n的数组,例如:

fruit_array=("banana" "orange")
Run Code Online (Sandbox Code Playgroud)

它应该看起来像这样:

  "foods": {
    "perishable": {
      "fruit": [
        { 
          "001": {
            "002": "banana"
          }
        },
        { 
          "001": {
            "002": "orange"
          }
        }
      ]
Run Code Online (Sandbox Code Playgroud)

有没有一个好的方法来做到这一点?目前我正在尝试以下操作:

#!/bin/bash

fruit_array=("banana" "orange")

for fruit in "${fruit_array[@]}"; do
   jq \
   --arg fruit $fruit \
   '.foods.perishables.fruit += [{"001": {"002": $fruit}}]' \
   template.json > template_with_fruit.json
done
Run Code Online (Sandbox Code Playgroud)

这不起作用的明显原因是模板正在被重新读取,但我已经搞砸了让它消耗上一次迭代的输出,但最后什么也没有出现。我只能更新模板一次。

然而,我知道这似乎有点狡猾,并怀疑有一种更干净、更jq 的方式。

之前的一次失败的尝试是这样的:

jq \
--argjson fruit …
Run Code Online (Sandbox Code Playgroud)

bash json jq

1
推荐指数
1
解决办法
458
查看次数

标签 统计

bash ×1

jq ×1

json ×1