我有以下 JMESPath 查询
query="Reservations[].Instances[].{ \
InstanceId: InstanceId, \
RootDeviceVolumeId: BlockDeviceMappings[?DeviceName==\`/dev/sda1\`] \
| [].Ebs.VolumeId | [0], \
RootDeviceName: RootDeviceName \
}"
aws ec2 describe-instances --query $query
Run Code Online (Sandbox Code Playgroud)
给出这样的输出
+------------+------------------+----------------------+
| InstanceId | RootDeviceName | RootDeviceVolumeId |
+------------+------------------+----------------------+
| i-12345678| /dev/sda1 | vol-abcdef12 |
| i-98765432| /dev/sda1 | vol-ef123456 |
| i-23456789| /dev/sda1 | vol-fedcba09 |
| i-aabbccdd| /dev/xvda | None |
+------------+------------------+----------------------+
Run Code Online (Sandbox Code Playgroud)
我想找到一种RootDeviceName从过滤器表达式中引用的方法BlockDeviceMappings,而不是对/dev/sda1设备名称进行硬编码,因为有时是/dev/xvda这样。但是,我找不到在过滤器表达式中引用父元素的方法。
另一种选择是将 and 映射RootDeviceName到InstanceId所有设备的投影上,然后将其通过管道传输到过滤器表达式,但语法似乎也不支持在投影中包含父元素。
我是否遗漏了某些内容,或者这只是 JMESPath 语法的限制?
有谁知道在下面的示例JSON中可以使用什么json查询过滤器来选择Tigger的食物?JSON是大规模且相对复杂的AWS blob的简化替代.
一些背景:我很高兴发现Ansible有一个json查询过滤器.鉴于我试图从AWS JSON blob中选择一个元素,这看起来就像我需要的那样.但是我很快遇到了麻烦,因为AWS对象有标签,我需要按标签选择项目.我尝试了相当于Foods[Tags[(Key='For') & (Value='Tigger')]]和类似的选择器路径,但没有设法让它工作.使用独立的JSON-查询库如https://www.npmjs.com/package/json-query我可以使用parent属性,但是,这并不似乎是在Ansible,撇开是从核心理念的偏差JSON查询.
回避问题并使用jsonpath选择器可能会更好.jsonpath类似于json-query,是xpath的翻译.
{ "Foods" :
[ { "Id": 456
, "Tags":
[ {"Key":"For", "Value":"Heffalump"}
, {"Key":"Purpose", "Value":"Food"}
]
}
, { "Id": 678
, "Tags":
[ {"Key":"For", "Value":"Tigger"}
, {"Key":"Purpose", "Value":"Food"}
]
}
, { "Id": 911
, "Tags":
[ {"Key":"For", "Value":"Roo"}
, {"Key":"Purpose", "Value":"Food"}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
参考文献:
我想用json-query来标记这个问题,但是缺乏这样做的要点.
The following JmesPath expression finds instances that have been tagged with a team:
"Instances[?Tags[?Key=='team']]"
Run Code Online (Sandbox Code Playgroud)
Do you know how to find instances that are not tagged with a team?
I have tried:
"Instances[?!Tags[?Key=='team']]"
-> !Tags[?Key=='team']]: event not found
"Instances[?null==Tags[?Key=='team']]"
-> [] <-- wrong answer
"Instances[?!not_null(Tags[?Key=='team'])]"
-> !not_null: event not found
Run Code Online (Sandbox Code Playgroud)
Many thanks in advance!
Sample input:
{ "Instances":
[ { "id": "i-911"
, "Tags":
[ {"Key":"owner", "Value":"Edu"}
, {"Key":"team", "Value":"forensics"}
]
, "many other keys": "stuff"
}
, { "id": "i-999" …Run Code Online (Sandbox Code Playgroud) 我有一个字典,其中package-name是键的字典,一些细节的字典是value:
{
"php7.1-readline": {
"latest": "7.1.9-1+ubuntu14.04.1+deb.sury.org+1",
"origins": [
"ppa.launchpad.net"
],
"version": "7.1.6-2~ubuntu14.04.1+deb.sury.org+1",
"www": "http://www.php.net/"
},
"php7.1-xml": {
"latest": "7.1.9-1+ubuntu14.04.1+deb.sury.org+1",
"origins": [
"ppa.launchpad.net"
],
"version": "7.1.6-2~ubuntu14.04.1+deb.sury.org+1",
"www": "http://www.php.net/"
},
"plymouth": {
"version": "0.8.8-0ubuntu17.1"
},
....
}
Run Code Online (Sandbox Code Playgroud)
我想将以上内容简化为仅包含latest其属性为-attribute 的包的字典。
似乎json_query是要使用的过滤器,但是我无法弄清楚语法。那里的例子似乎都是在词典列表上操作的,而不是相同词典的列表 ...
例如,如果我将上述字典“管道”到中json_query('*.latest'),我会得到实际最新版本的列表:
[
"7.1.9-1+ubuntu14.04.1+deb.sury.org+1",
"7.1.9-1+ubuntu14.04.1+deb.sury.org+1",
"7.1.6-2~ubuntu14.04.1+deb.sury.org+1"
]
Run Code Online (Sandbox Code Playgroud)
我如何获取整个字典元素呢?
有希望吗
我有一个 JSON
{
"key": "processId-29231",
"fields": {
"attachment": [
{
"id": "79572",
"filename": "File1.png"
},
{
"id": "74620",
"filename": "File2.docx"
},
{
"id": "79072",
"filename": "File3.xlsx"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将其重组为这个
{
"processId": "processId-29231",
"attachments": [
"https://example.com/files/79572/File1.png",
"https://example.com/files/79572/File2.docx",
"https://example.com/files/79572/File1.xlsx",
]
}
Run Code Online (Sandbox Code Playgroud)
我可以使用特定的数组索引来完成这项工作
{processID:key,attachments:join('',['https://example.com/files/',fields.attachment[1].id,'/',fields.attachment[1].filename])}
Run Code Online (Sandbox Code Playgroud)
这产生了这个结果
{
"processID": "processId-29231",
"attachments": "https://example.com/files/74620/File2.docx"
}
Run Code Online (Sandbox Code Playgroud)
我以两种方式在没有数组索引的情况下尝试了这个
这个
{processID:key,attachments:join('',['https://example.com/',fields.attachment[].id,'/',fields.attachment[].filename])}
Run Code Online (Sandbox Code Playgroud)
和这个
{processID:key,attachments:join('', ['https://example.com/',fields.attachment[*].id,'/',fields.attachment[*].filename])}
Run Code Online (Sandbox Code Playgroud)
但这没有帮助。
有关如何解决此问题的任何提示?
作为我上一个问题的后续问题,我无法让标签在这种类型的输出中正常工作。我想打印一个表格,其中每个属性作为一行。这就是我期望它的样子:
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Type', Value: InstanceType }]"
-------------------------------
| DescribeInstances |
+-----------+-----------------+
| Property | Value |
+-----------+-----------------+
| Type | g4dn.12xlarge |
+-----------+-----------------+
Run Code Online (Sandbox Code Playgroud)
但对于标签名称来说,它看起来像这样:
% 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 ||
|+---------------+|
Run Code Online (Sandbox Code Playgroud)
标签值是正确的,但格式很奇怪,当与更多其他行组合时,表格变得非常难看。
下面是一个 JSON 示例:
{
"School": [
{"@id": "ABC_1",
"SchoolType": {"@tc": "10023204",
"#text": "BLUE FOX"}},
{"@id": "ABC_2",
"SchoolType": {"@tc": "143", "#text": "AN EAGLE"}},
{"@id": "ABC_3",
"SchoolType": {"@tc": "21474836", "#text": "OTHER REASONS"},
"SchoolStatus": {"@tc": "21474836", "#text": "FINE"},
"Teacher": [
{"@id": "XYZ_1",
"TeacherType": {"@tc": "5", "#text": "GENDER"},
"Gender": "FEMALE",
"Extension": {"@VC": "23",
"Extension_Teacher": {"DateDuration": {"@tc": "10023111",
"#text": "0-6 MONTHS"}}}},
{"@id": "XYZ_2",
"TeacherType": {"@tc": "23", "#text": "EDUCATED"},
"Extension": {"@VC": "23",
"Extension_Teacher": {"DateDuration": {"@tc": "10023111",
"#text": "CURRENT"}}}}]},
{"@id": "ABC_4",
"SchoolType": {"@tc": "21474836", "#text": "OTHER DAYS"}, …Run Code Online (Sandbox Code Playgroud) 在新的AWS控制台中,我可以进行反向搜索,即:使用过滤器搜索与特定值不匹配的搜索。
我正在尝试使用 AWS cli v2 来实现相同的目标。按标签过滤很简单,但如何同时排除标签?
aws ec2 describe-instances \
--query 'Reservations[].Instances[].[InstanceId]' \
--filter "Name=tag:MYTAG,Values=MYVALUE" \
--output text
Run Code Online (Sandbox Code Playgroud)
我试图获取具有特定标签的实例列表,但同时根据另一个标签排除其中一些实例。
例如:假设我有带有以下标签的实例:
tag: env | value: dev
tag: eks | value: true
Run Code Online (Sandbox Code Playgroud)
我想构建一个具有env/键值对的实例列表,但如果它们也具有/键值对,则dev希望排除它们。ekstrue
我可以看到 JMESPath 有一个布尔值,contains但我正在努力使用它。
我正在尝试根据某些标签条件在我的 AWS 中查找资源。我一直在使用jq它来实现它,但是有没有使用的方法resourcegroupstaggingapi,因此它们提供了一种直接的方法来过滤缺少任何指定标签的资源。
我有 2 个用例,这就是我使用 jq 过滤它们的方式:
aws resourcegroupstaggingapi get-resources --tags-per-page 100 | jq '.ResourceTagMappingList | map(select(.Tags[] | select(.Key == "SV") and select(.Key != "DM")))'aws resourcegroupstaggingapi get-resources --tags-per-page 100 | jq '.ResourceTagMappingList | map(select(.Tags[] | (.Key | IN("CostCenter", "Environment", "Team",) | not)))'我尝试在不使用 jq 的情况下执行相同的操作,如下所示:
aws resourcegroupstaggingapi get-resources --tag-filters "Key=SV,Values=*" "Key=DM,Values=NULL"aws resourcegroupstaggingapi get-resources --tag-filters "Key=CostCenter,Values=" "Key=Environment,Values=" "Key=Team,Values="但是使用 jq …
使用JMESPath,是否可以根据输入中存在多个键/值对来过滤输出?
从下面的示例JSON中,我想要做的只是提取包含这些键/值对的对象Tags-
环境/ ABC
项目/项目2
我能得到的最接近的是选择一个匹配的标签,但我也要求对象的其余部分,我还需要匹配另一个键/值对 -
Stacks[*].Tags[?Key=='Environment' && Value=='ABC']
Run Code Online (Sandbox Code Playgroud)
这里有一些exathle JSON输入 -
{
"Stacks": [
{
"StackId": "abc123",
"Tags": [
{
"Value": "Project 1",
"Key": "Project"
},
{
"Value": "ABC",
"Key": "Environment"
}
],
"CreationTime": "2016-07-20T14:49:27.891Z",
"StackName": "TestStack1",
"NotificationARNs": [],
"StackStatus": "CREATE_COMPLETE",
"DisableRollback": false
},
{
"StackId": "xyz123",
"Tags": [
{
"Value": "Project 1",
"Key": "Project"
},
{
"Value": "XYZ",
"Key": "Environment"
}
],
"CreationTime": "2016-07-20T14:49:27.891Z",
"StackName": "TestStack2",
"NotificationARNs": [],
"StackStatus": "CREATE_COMPLETE",
"DisableRollback": false
},
{
"StackId": …Run Code Online (Sandbox Code Playgroud)