英文:
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
{
"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"
},
So far i've tried aws ec2 describe-instances | jq '.[] | {publicIP : .Reservations.Instances[].PublicIpAddress}'
as well as aws ec2 describe-instances | jq '.[] | {publicIP : .Reservations.Instances.PublicIpAddress}'
but i keep running into jq: error (at <stdin>:151): Cannot index array with string "Reservations"
答案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:
{
"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:
.Reservations[].Instances[].PublicIpAddress | { publicIp: . }
.Reservations[] | { publicIp: .Instances[].PublicIpAddress }
{ publicIp: .Reservations[].Instances[].PublicIpAddress }
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论