2017-05-16 81 views
1

我想使用jq從JSON對象返回多個元素。該數據從AWS CLI查找的到來,這是輸出的只是一小部分:使用jq返回多個元素?

{ 
"ReservedInstancesOfferings": [{ 
     "OfferingClass": "convertible", 
     "OfferingType": "No Upfront", 
     "AvailabilityZone": "ap-southeast-2a", 
     "InstanceTenancy": "default", 
     "PricingDetails": [], 
     "ProductDescription": "Linux/UNIX", 
     "UsagePrice": 0.0, 
     "RecurringCharges": [{ 
      "Amount": 0.167, 
      "Frequency": "Hourly" 
     }], 
     "Marketplace": false, 
     "CurrencyCode": "USD", 
     "FixedPrice": 0.0, 
     "Duration": 94608000, 
     "Scope": "Availability Zone", 
     "ReservedInstancesOfferingId": "1fc8c02b-bcc0-42b8-82ef-47c6f6d1c1b5", 
     "InstanceType": "c4.xlarge" 
    }, 
    { 
     "OfferingClass": "convertible", 
     "OfferingType": "No Upfront", 
     "AvailabilityZone": "ap-southeast-2a", 
     "InstanceTenancy": "dedicated", 
     "PricingDetails": [], 
     "ProductDescription": "Red Hat Enterprise Linux", 
     "UsagePrice": 0.0, 
     "RecurringCharges": [{ 
      "Amount": 0.243, 
      "Frequency": "Hourly" 
     }], 
     "Marketplace": false, 
     "CurrencyCode": "USD", 
     "FixedPrice": 0.0, 
     "Duration": 94608000, 
     "Scope": "Availability Zone", 
     "ReservedInstancesOfferingId": "24aaceee-f54e-4882-aba3-ce710a5036c9", 
     "InstanceType": "c4.xlarge" 
    } 
] 

}

我想提取字段:

.ReservedInstancesOfferings[].ProductDescription 
.ReservedInstancesOfferings[].InstanceType 
.ReservedInstancesOfferings[].RecurringCharges[].Amount 

我會喜歡輸出像這樣:

"Red Hat Enterprise Linux" 
"c4.xlarge" 
0.183 

正如你可以看到上面我明白如何提取所有元素指標單獨的,但我無法弄清楚如何把它們放在一起來達到預期的輸出。我已經修正了用逗號分隔查詢,但那不提供所需的輸出。

任何幫助將非常感激。

謝謝!

編輯:

哦,哦,我明白了!完整答案如下。男孩哦,男孩jq是強大的!

希望這可以幫助別人:)

+0

您應該添加自己的答案,而不僅僅是編輯問題。 –

+0

是的,我有點興奮:)答案在下面。 –

回答

2

好吧,所以我設法得到我以後的東西。我還是有一點雜物車做一些輸入字段不完全一致,但它是足夠接近向前帶:

aws ec2 describe-reserved-instances-offerings --availability-zone "ap-southeast-2a" --instance-type "c4.xlarge" | 
jq '.ReservedInstancesOfferings[] | [.ProductDescription, .InstanceType, .RecurringCharges[].Amount]' 

輸出:

[ 
    "Linux/UNIX", 
    "c4.xlarge", 
    0.167 
] 
[ 
    "Red Hat Enterprise Linux", 
    "c4.xlarge", 
    0.243 
] 
[ 
    "Red Hat Enterprise Linux", 
    "c4.xlarge", 
    0.227 
] 
[ 
    "Linux/UNIX", 
    "c4.xlarge", 
    0.183 
] 
[ 
    "SUSE Linux", 
    "c4.xlarge", 
    0.2 
]