【问题标题】:Getting value from json parameter doesn't work (jq: 1 compile error)从 json 参数获取值不起作用(jq:1 编译错误)
【发布时间】:2018-06-27 06:27:39
【问题描述】:

我正在使用 awscli 并尝试从查询的输出中获取 IpAddress 的值。

我尝试使用 jq,但出现编译错误。

是这样的:

output="$(aws efs describe-mount-targets --file-system-id fs-089b5e31)"
echo $output
{ "MountTargets": [ { "MountTargetId": "fsmt-bb29e666", "IpAddress": "172.20.33.255", "OwnerId": "668225551666", "SubnetId": "subnet-0b61377039d31e666", "NetworkInterfaceId": "eni-045f6ea1376662bdf", "FileSystemId": "fs-089b5e66", "LifeCycleState": "available" } ] }

这是我用来获取 IpAddress 的命令:

echo array | jq '.[]MountTarget[]s.IpAddress'

我得到的错误是这样的:

parse error: Invalid numeric literal at line 2, column 0
ubuntu@ip-10-10-16-245:~/infra-devops/kops/vector$ echo array | jq '.[]MountTarget[]s.IpAddress'
jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.[]MountTarget[]s.IpAddress
jq: 1 compile error

问题是我的查询还是我最好改用 sed?

【问题讨论】:

    标签: sed syntax syntax-error jq


    【解决方案1】:

    您访问数组的语法错误。要获取 IP 地址,请使用:

    aws efs describe-mount-targets --file-system-id fs-089b5e31 | 
      jq '.MountTargets[0].IpAddress'
    

    MountTargets 是一个数组,您希望从中获取第一个对象。

    如果您需要原始数据(不带双引号),请在jq 命令中使用-r 选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      相关资源
      最近更新 更多