问题描述
作为我的 previous question 的后续行动,我无法让标签在这种类型的输出中正常工作。我想将每个属性作为一行打印一个表格。这是我期望的样子:
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Type',Value: InstanceType }]"
-------------------------------
| DescribeInstances |
+-----------+-----------------+
| Property | Value |
+-----------+-----------------+
| Type | g4dn.12xlarge |
+-----------+-----------------+
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Name',Value: Tags[?Key =='Name'].Value }]"
-------------------
|DescribeInstances|
+-----------------+
| Property |
+-----------------+
| Name |
+-----------------+
|| Value ||
|+---------------+|
|| Elliott-TKD ||
|+---------------+|
标签值是正确的,但格式很奇怪,当结合更多其他行时,表格变得非常难看。
解决方法
查询的过滤器部分 ([?Key == 'Name']
) 正在创建 JMESPath 所描述的 projection。
您必须重置此投影才能从中提取单个字符串。
可以使用 pipes 来重置投影。
投影是 JMESPath 中的一个重要概念。但是,有时投影语义不是您想要的。一个常见的场景是当您想要对投影的结果进行运算而不是将表达式投影到数组中的每个元素上时。
例如,表达式 people[*].first
将为您提供一个数组,其中包含 people 数组中每个人的名字。如果您想要该列表中的第一个元素怎么办?如果您尝试 people[*].first[0]
只是为 people 数组中的每个元素计算 first[0]
,并且由于没有为字符串定义索引,最终结果将是一个空数组 []
。要实现所需的结果,您可以使用管道表达式 <expression> | <expression>
来指示必须停止投影。
来源:https://jmespath.org/tutorial.html#pipe-expressions
因此您的问题与他们在文档中描述的非常接近,并且可以使用以下方法重置该投影:
Tags[?Key =='Name']|[0].Value
或者,使用:
Tags[?Key =='Name'].Value | [0]
这是两个完全相同的查询。
给定 JSON:
{
"Reservations": [
{
"Instances": [
{
"Tags": [
{
"Key": "Name","Value": "Elliott-TKD"
},{
"Key": "Foo","Value": "Bar"
}
]
}
]
}
]
}
查询
Reservations[].Instances[0].[{ Property: `Name`,Value: Tags[?Key == `Name`]|[0].Value }]
会给预期的
[
[
{
"Property": "Name","Value": "Elliott-TKD"
}
]
]
所以它会在你的表格中正确呈现
------------------------------
| DescribeInstance |
+------------+---------------+
| Property | Value |
+------------+---------------+
| Name | Elliott-TKD |
+------------+---------------+