将JSON数组中的值映射到字典的最简单方法是使用jq吗?

huangapple go评论59阅读模式
英文:

What's the easiest way to map JSON array values to a dictionary using jq?

问题

You can use the following jq command to create a key-value dictionary from the given JSON data:

cat api_fm.json | jq -r 'reduce range(0, length/2) as $i ({}; . + { (.[$i]): .[$(length/2 + $i)] }) | to_entries | map("\(.key): \(.value)")'

This command will produce the desired output:

[
  "plugin: Started",
  "c_docker_2: Started",
  "c_docker_5: Started",
  "c_docker_4: Started",
  "c_docker_3: Started",
  "c_docker_1: Started"
]
英文:

How can I make a key value dictionary using jq?

If I do: cat api_fm.json | jq -r "[ .[].name, .[].status ]"
I receive:

[
  "plugin",
  "c_docker_2",
  "c_docker_5",
  "c_docker_4",
  "c_docker_3",
  "c_docker_1",
  "Started",
  "Started",
  "Started",
  "Started",
  "Started",
  "Started"
]

So, I would like to map the name with the status like this:

[
   "plugin: started"
...
]

答案1

得分: 0

这是一种方式:

jq 'map("\(.name): \(.status)")'
英文:

Here is one way:

jq 'map("\(.name): \(.status)")'

答案2

得分: 0

以下是您要求的内容的翻译:

$ cat api_fm.json
[
  {"name":"plugin", "status":"已启动"},
  {"name":"c_docker_2", "status":"已启动"},
  {"name":"c_docker_5", "status":"已启动"},
  {"name":"c_docker_4", "status":"已启动"},
  {"name":"c_docker_3", "status":"已启动"},
  {"name":"c_docker_1", "status":"已启动"}
]

然后,要创建Bash关联数组的部分:

declare -A prod

while IFS=$'\t' read -r name status; do
    prod["$name"]=$status
done < <(
    jq -r '.[] | [.name, .status] | @tsv' api_fm.json
)

declare -p prod
declare -A prod=([c_docker_2]="已启动" [c_docker_3]="已启动" [c_docker_1]="已启动" [c_docker_4]="已启动" [c_docker_5]="已启动" [plugin]="已启动" )
英文:

Working through the requirements in your comment. Given:

$ cat api_fm.json
[
  {&quot;name&quot;:&quot;plugin&quot;, &quot;status&quot;:&quot;Started&quot;},
  {&quot;name&quot;:&quot;c_docker_2&quot;, &quot;status&quot;:&quot;Started&quot;},
  {&quot;name&quot;:&quot;c_docker_5&quot;, &quot;status&quot;:&quot;Started&quot;},
  {&quot;name&quot;:&quot;c_docker_4&quot;, &quot;status&quot;:&quot;Started&quot;},
  {&quot;name&quot;:&quot;c_docker_3&quot;, &quot;status&quot;:&quot;Started&quot;},
  {&quot;name&quot;:&quot;c_docker_1&quot;, &quot;status&quot;:&quot;Started&quot;}
]

We can produce tab-separated name-status pairs like

$ jq -r &#39;.[] | [.name, .status] | @tsv&#39; api_fm.json
plugin	Started
c_docker_2	Started
c_docker_5	Started
c_docker_4	Started
c_docker_3	Started
c_docker_1	Started

Then, to create the bash associative array

declare -A prod

while IFS=$&#39;\t&#39; read -r name status; do
    prod[&quot;$name&quot;]=$status
done &lt; &lt;(
    jq -r &#39;.[] | [.name, .status] | @tsv&#39; api_fm.json
)

declare -p prod
declare -A prod=([c_docker_2]=&quot;Started&quot; [c_docker_3]=&quot;Started&quot; [c_docker_1]=&quot;Started&quot; [c_docker_4]=&quot;Started&quot; [c_docker_5]=&quot;Started&quot; [plugin]=&quot;Started&quot; )

huangapple
  • 本文由 发表于 2023年5月25日 18:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定