使用jq返回多个元素?

时间:2017-05-16 07:25:42

标签: json jq

我正在尝试使用jq从JSON对象返回多个元素。数据来自AWS cli查找,这只是输出的一小部分:

<div *ngIf="exceptions">
    <h1 class="applicationTitle">Exceptions</h1>

    <table class="table table-hover table-condensed">
        <th>Date</th>
        <th>Timeslot</th>

        <tr *ngFor="#exception of exceptions">
            <td>{{exception.date}}</td>
            <td>{{exception.value}}</td>
        </tr>
    </table>
</div>

}

我正在尝试提取字段:

{
"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

正如您在上面所看到的,我理解如何单独提取所有元素,但我无法弄清楚如何将它们全部放在一起以获得所需的输出。我已经用逗号分隔查询,但这并没有提供所需的输出。

非常感谢任何帮助。

谢谢!

编辑:

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

希望这有助于某人:)

1 个答案:

答案 0 :(得分:3)

好的,所以我设法得到了我想要的东西。我仍然有点整理,因为一些输入字段并不完全统一,但它足够接近以前继续:

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
]