jq-将json作为bash数组返回 [英] jq - return json as bash array

查看:94
本文介绍了jq-将json作为bash数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于

My question is similar to this question but I need something more to be done and can't quite figure out how to do.

这是我的JSON字符串

This is my JSON string

{
  "value": "1"
}
{
  "value": "3"
}
{
  "value": "4"
}

我需要用BASH编写脚本以输出

I need to write a script in BASH to output

  • 2-如果序列缺少数字
  • 5-如果序列不缺少数字

在上面的示例中,json数组中缺少2.并且脚本应返回2.如果数组中存在2,则应返回5.

In the above example, 2 is missing from the json array. And the script should return 2. If 2 is present in the array it should return 5.

我认为我可能能够使用bash中的while循环编写逻辑以增加数字,但是我被困在这一点上,我无法弄清楚如何仅将JSON字符串转换为bash数组值.

I think I may be able to write the logic to increment the number using a while loop in bash, but I am stuck at this point where I can't figure out how to convert this JSON string to bash array with just the value.

下面是我用来获取JSON输出的确切命令.这是AWS CLI命令,用于从具有特定TAG的AWS获取所有实例.

Below is the exact command that I used to get the JSON output. This is AWS CLI command to get all the instances from AWS that has a specific TAG.

readarray -t arr < <(aws ec2 describe-instances --region=us-east-1 --filters --filters "Name=tag:NodeType,Values=worker" --query "Reservations[].Instances[].Tags[]" | jq -r '.[] | select(.Key == "NodeNumber") | {value: .Value}')
printf '%s\n' "${arr[@]}"

以上返回我

{
  "value": "1"
}
{
  "value": "3"
}
{
  "value": "4"
}

但是,我只需要将VALUE字段"value"作为bash数组即可

However, I need to get just the VALUE field "value" as a bash array

推荐答案

首先,存储数据

给出您的原始数据,将其作为字符串存储在 json 变量中,也许使用以下文档:

First, Store the Data

Given your raw data stored as a string in a json variable, perhaps with a here-document:

json=$(
    cat <<- EOF
        {
          "value": "1"
        }
        {
          "value": "3"
        }
        {
          "value": "4"
        }
EOF
)

Bash本身会做一个合理的美化工作:

Bash itself will do a reasonable job of prettifying it:

$ echo $json
{ "value": "1" } { "value": "3" } { "value": "4" }

将数据解析为Bash数组

有多种方法可以做到这一点.两种比较明显的方法是使用 jq grep 将这些值提取到具有外壳的简单数组表示法的Bash数组中:

Parsing the Data Into a Bash Array

There's more than one way to do this. Two of the more obvious ways are to use use jq or grep to extract the values into a Bash array with the shell's simple array notation:

values=( `echo $json | jq .value` )
echo "${values[@]}"
"1" "3" "4"

unset values
values=$(egrep -o '[[:digit:]]+' <<< "$json")
echo "${values[@]}"
1
3
4

当然,还有其他方法可以完成此任务,但这似乎很容易完成.您的里程可能会有所不同.

There are certainly other ways to accomplish this task, but this seems like the low-hanging fruit. Your mileage may vary.

关于Bash数组要记住的主要事情是,如果您想将它们用于循环而不是直接对其进行索引,则它们需要使用加引号的"${foo[@]}"或不加引号的${bar[*]}之类的扩展.但是,一旦它们位于数组中,只要您了解不同的扩展名和引用规则,就可以轻松访问整个数组.

The main thing to remember about Bash arrays is that they need to use expansions such as "${foo[@]}" when quoted, or ${bar[*]} when unquoted, if you want to use them for loops rather than indexing into them directly. Once they're in an array, though, you can access the entire array easily as long as you understand the different expansions and quoting rules.

这篇关于jq-将json作为bash数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆