简体   繁体   English

使用 bash 使用 jq 循环从变量构建嵌套的 json 数据

[英]Build nested json data from variable with looping with jq using bash

I'm trying to create a dynamic json data using jq, but I don't know how to write it when involving loop.我正在尝试使用jq创建一个动态的json数据,但我不知道在涉及循环时如何编写它。 I can do this in normal bash writing a sequence string我可以在普通的 bash 中写一个序列字符串来做到这一点

Here is the example code:这是示例代码:

#!/bin/bash

# Here I declare 2 arrays just to demonstrate. Each values pairs between `disk_ids` and `volume_ids` cannot have the same value. If one has value, the other must null.

disk_ids=(111 null 222 333)
volume_ids=(null 444 null null)
json_query_data=""

# Now example I want to loop through the above 2 arrays and create json. In real application I already have the above arrays that is looping where I can access using $disk_id and $volume_id.

for disk_id in "${disk_ids[@]}"; do
  for volume_id in "${volume_ids[@]}"; do
    json_query_data=$(jq -n --argjson disk_id "$disk_id" --argjson volume_id "$volume_id" '{
                          devices: {
                                             sda: {"disk_id": $disk_id, "volume_id": $volume_id },
                                             sdb: {"disk_id": $disk_id, "volume_id": $volume_id },
                                             sdc: {"disk_id": $disk_id, "volume_id": $volume_id },
                                             sdd: {"disk_id": $disk_id, "volume_id": $volume_id },
                                          }}')

  done
done

As you can see that is definitely NOT the output that I want, and my code writing logic is not dynamic.如您所见,这绝对不是我想要的输出,而且我的代码编写逻辑也不是动态的。 The final output should produce the following json when I echo "${json_query_data}" :当我echo "${json_query_data}"时,最终输出应该产生以下 json:

{
    devices: {
               sda: {"disk_id": 111, "volume_id": null },
               sdb: {"disk_id": null, "volume_id": 444 },
               sdc: {"disk_id": 222, "volume_id": null },
               sdd: {"disk_id": 333, "volume_id": null },
}}

I have not seen any example online regarding looping with variable when creating json data with jq .在使用jq创建 json 数据时,我还没有在网上看到任何关于使用变量循环的示例。 Appreciate if someone can help.感谢有人可以提供帮助。 Thanks.谢谢。

UPDATE:更新:

I must use for loop inside bash to create the json data.我必须在 bash 中使用 for 循环来创建 json 数据。 Because the sample array disk_ids and volume_ids that I provided in the code were just example, In real application I already able to access the variable $disk_id and $volume_id for each for loop counter.因为我在代码中提供的示例数组disk_idsvolume_ids只是示例,在实际应用程序中我已经能够访问每个 for 循环计数器的变量$disk_id$volume_id But how do I use this variables and create the json output that fill up all the data above inside that for loop?但是我如何使用这个变量并创建 json 输出来填充 for 循环中上面的所有数据?

The json example is taken from: linode API here json示例取自: linode API here

The looping/mapping can also be accomplished in jq:循环/映射也可以在 jq 中完成:

#!/bin/bash

disk_ids=(111 null 222 333)
volume_ids=(null 444 null null)

jq -n --arg disk_ids "${disk_ids[*]}" --arg volume_ids "${volume_ids[*]}" '
  [$disk_ids, $volume_ids | . / " " | map(fromjson)]
  | transpose | {devices: with_entries(
      .key |= "sd\([. + 97] | implode)"
      | .value |= {disk_id: first, volume_id: last}
    )}
'

Demo演示

Or, if you can already provide the letters in the same way:或者,如果您已经可以用相同的方式提供字母:

#!/bin/bash

disk_ids=(111 null 222 333)
volume_ids=(null 444 null null)
letters=(a b c d)

jq -n --arg disk_ids "${disk_ids[*]}" --arg volume_ids "${volume_ids[*]}" --arg letters "${letters[*]}" '
  [$disk_ids, $letters, $volume_ids | . / " " ] | .[0,2] |= map(fromjson)
  | transpose | {devices: with_entries(
      .key = "sd\(.value[1])"
      | .value |= {disk_id: first, volume_id: last}
    )}
'

Demo演示

Output:输出:

{
  "devices": {
    "sda": {
      "disk_id": 111,
      "volume_id": null
    },
    "sdb": {
      "disk_id": null,
      "volume_id": 444
    },
    "sdc": {
      "disk_id": 222,
      "volume_id": null
    },
    "sdd": {
      "disk_id": 333,
      "volume_id": null
    }
  }
}

UPDATE:更新:

I must use for loop inside bash to create the json data.我必须在 bash 中使用 for 循环来创建 json 数据。

If you insist on doing this in a bash loop, how about:如果您坚持在 bash 循环中执行此操作,那么:

#!/bin/bash

disk_ids=(111 null 222 333)
volume_ids=(null 444 null null)
json='{"devices": {}}'

for i in ${!disk_ids[@]}
do
  json="$(
    jq --argjson disk_id "${disk_ids[$i]}" --argjson volume_id "${volume_ids[$i]}" '
      .devices |= . + {"sd\([length + 97] | implode)": {$disk_id, $volume_id}}
    ' <<< "$json"
  )"
done
echo "$json"

Or, with letters included:或者,包含字母:

#!/bin/bash

disk_ids=(111 null 222 333)
volume_ids=(null 444 null null)
letters=(a b c d)
json='{"devices": {}}'

for i in ${!disk_ids[@]}
do
  json="$(
    jq --argjson disk_id "${disk_ids[$i]}" --argjson volume_id "${volume_ids[$i]}" --arg letter "${letters[$i]}" '
      .devices["sd\($letter)"] += {$disk_id, $volume_id}
    ' <<< "$json"
  )"
done
echo "$json"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM