如何使用jq从JSON中获取值?

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

How to get value from json using jq?

问题

我对 jq 还不熟悉,并且在尝试获取 JSON 响应中的值时遇到了一些问题。我想要获取 PublicIpAddress 的值。

{
  "Reservations": [
    {
      "Groups": [],
      "Instances": [
        {
          "AmiLaunchIndex": 0,
          "ImageId": "ami-XXX",
          "InstanceId": "i-XXX",
          "InstanceType": "t2.micro",
          "KeyName": "dev",
          "LaunchTime": "2022-06-04T22:30:04+00:00",
          "Monitoring": {
            "State": "disabled"
          },
          "Placement": {
            "AvailabilityZone": "us-east-1d",
            "GroupName": "",
            "Tenancy": "default"
          },
          "PrivateDnsName": "ip-172-28-99-112.ec2.internal",
          "PrivateIpAddress": "172.28.99.112",
          "ProductCodes": [],
          "PublicDnsName": "ec2-54-11-101-63.compute-1.amazonaws.com",
          "PublicIpAddress": "54.11.101.63",
          "State": {
            "Code": 16,
            "Name": "running"
          }
        }
      ]
    }
  ]
}

到目前为止,我尝试过 aws ec2 describe-instances | jq '.[] | {publicIP : .Reservations.Instances[].PublicIpAddress}' 以及 aws ec2 describe-instances | jq '.[] | {publicIP : .Reservations.Instances.PublicIpAddress}',但我一直遇到 jq: error (at <stdin>:151): Cannot index array with string "Reservations"

英文:

I am new to jq and running into a bit of an issue trying to get a value from the json response. I want to get the value of the PublicIpAddress

{
  &quot;Reservations&quot;: [
    {
      &quot;Groups&quot;: [],
      &quot;Instances&quot;: [
        {
          &quot;AmiLaunchIndex&quot;: 0,
          &quot;ImageId&quot;: &quot;ami-XXX&quot;,
          &quot;InstanceId&quot;: &quot;i-XXX&quot;,
          &quot;InstanceType&quot;: &quot;t2.micro&quot;,
          &quot;KeyName&quot;: &quot;dev&quot;,
          &quot;LaunchTime&quot;: &quot;2022-06-04T22:30:04+00:00&quot;,
          &quot;Monitoring&quot;: {
            &quot;State&quot;: &quot;disabled&quot;
          },
          &quot;Placement&quot;: {
            &quot;AvailabilityZone&quot;: &quot;us-east-1d&quot;,
            &quot;GroupName&quot;: &quot;&quot;,
            &quot;Tenancy&quot;: &quot;default&quot;
          },
          &quot;PrivateDnsName&quot;: &quot;ip-172-28-99-112.ec2.internal&quot;,
          &quot;PrivateIpAddress&quot;: &quot;172.28.99.112&quot;,
          &quot;ProductCodes&quot;: [],
          &quot;PublicDnsName&quot;: &quot;ec2-54-11-101-63.compute-1.amazonaws.com&quot;,
          &quot;PublicIpAddress&quot;: &quot;54.11.101.63&quot;,
          &quot;State&quot;: {
            &quot;Code&quot;: 16,
            &quot;Name&quot;: &quot;running&quot;
          },

So far i've tried aws ec2 describe-instances | jq &#39;.[] | {publicIP : .Reservations.Instances[].PublicIpAddress}&#39; as well as aws ec2 describe-instances | jq &#39;.[] | {publicIP : .Reservations.Instances.PublicIpAddress}&#39; but i keep running into jq: error (at &lt;stdin&gt;:151): Cannot index array with string &quot;Reservations&quot;

答案1

得分: 1

I think you want the following:

{
  "publicIp": "54.11.101.63"
}

Note that multiple elements in any of the arrays will produce separate output elements.

Alternative spellings of the same program:

{ "publicIp": "54.11.101.63" }
{ "publicIp": "54.11.101.63" }
{ "publicIp": "54.11.101.63" }

But maybe you are really looking for this?

jq -r '.Reservations[].Instances[].PublicIpAddress'

Which will simply output 54.11.101.63 (one IP address per line, if there are multiple in your input document).

英文:

I think you want the following:

.Reservations[].Instances[] | { publicIp: .PublicIpAddress }

Output:

{
  &quot;publicIp&quot;: &quot;54.11.101.63&quot;
}

Note that multiple elements in any of the arrays will produce separate output elements.

Alternative spellings of the same program:

.Reservations[].Instances[].PublicIpAddress | { publicIp: . }
.Reservations[] | { publicIp: .Instances[].PublicIpAddress }
{ publicIp: .Reservations[].Instances[].PublicIpAddress }

But maybe you are really looking for this?

jq -r &#39;.Reservations[].Instances[].PublicIpAddress&#39;

Which will simply output 54.11.101.63 (one IP address per line, if there are multiple in your input document).

huangapple
  • 本文由 发表于 2023年6月5日 07:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402740.html
匿名

发表评论

匿名网友

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

确定