使用JMESpath构建对象 [英] Object construction with JMESpath

查看:68
本文介绍了使用JMESpath构建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS CLI构建JMESpath查询,该查询打印一个表,该表以行的形式显示一些选定的属性.我可以使用 jq 得到我想要的东西,但是我想只用 awscli 来做到这一点,以便它可以格式化为表格.这可能吗?下面是我想要的输出,使用 jq 对象构造语法:

I'm trying to build a JMESpath query with AWS CLI that prints a table showing a few selected properties as rows. I can get what I want using jq but I want to do it with just awscli so that it can format as a table. Is this possible? Below is the output I want, using jq object consrtuction syntax:

% aws --output json ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[0]' | jq '.[0] | {InstanceType,PrivateIpAddress,LaunchTime}'
{
  "InstanceType": "g4dn.4xlarge",
  "PrivateIpAddress": "172.31.15.37",
  "LaunchTime": "2021-02-17T14:49:30+00:00"
}

最接近的是使用多重选择哈希,但这会使每个项目都成为一列,因此,如果有多个项目,那么看起来就不好了.

The closest I have come is this using a multiselect hash, but this makes each item a column, so it does not look good if there are more than a few items.

% aws --output table ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[0].{size: InstanceType, PrivateIP: PrivateIpAddress, LaunchTime: LaunchTime}' 
---------------------------------------------------------------
|                      DescribeInstances                      |
+---------------------------+----------------+----------------+
|        LaunchTime         |   PrivateIP    |     size       |
+---------------------------+----------------+----------------+
|  2021-02-17T14:49:30+00:00|  172.31.15.37  |  g4dn.4xlarge  |
+---------------------------+----------------+----------------+

推荐答案

table 输出会将不同的JSON对象视为不同的行.
如果确实打算每行都有一个属性,则可以使用以下JMESPath查询为每个属性创建一个对象:

The table output will consider different JSON objects as different row.
If you indeed intend to have a property per row, you can create an object per property with a JMESPath query like this:

Reservations[].Instances[0].[ { Property: `LaunchTime`, Value: LaunchTime }, { Property: `Size`, Value: InstanceType }, { Property: `PrivateIP`, Value: PrivateIpAddress } ]

在类似JSON的结构上

On a JSON structure like:

{
  "Reservations": [
    {
      "Instances": [
        {
          "InstanceType": "g4dn.4xlarge",
          "PrivateIpAddress": "172.31.15.37",
          "LaunchTime": "2021-02-17T14:49:30+00:00"
        }
      ]
    }
  ]
}

这将为您提供以下JSON:

This will give you the this JSON as a result:

[
  [
    {
      "Property": "LaunchTime",
      "Value": "2021-02-17T14:49:30+00:00"
    },
    {
      "Property": "Size",
      "Value": "g4dn.4xlarge"
    },
    {
      "Property": "PrivateIP",
      "Value": "172.31.15.37"
    }
  ]
]

然后表格应如下所示:

----------------------------------------------
|              DescribeInstances             |
+--------------+-----------------------------+
|   Property   |            Value            |
+--------------+-----------------------------+
|  LaunchTime  |  2021-02-17T14:49:30+00:00  |
+--------------+-----------------------------+
|     Size     |         g4dn.4xlarge        |
+--------------+-----------------------------+
|  PrivateIP   |         172.31.15.37        |
+--------------+-----------------------------+

这篇关于使用JMESpath构建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆