JMESPath JSON filter with multiple matches
Asked Answered
O

2

30

I have a json block that looks a bit like this (have you guessed from AWS)

{ "Vpcs":[ 
  {
    "VpcId":"vpc-blabla1",
    "OtherKey":"Value"
  },
  {
    "VpcId":"vpc-blabla2",
    "OtherKey":"Value"
  },
  {
    "VpcId":"vpc-blabla3",
    "OtherKey":"Value"
  },
  {
    "VpcId":"vpc-blabla4",
    "OtherKey":"Value"
  }]
}

I want to use JMESPath to get the OtherKey value for vpc-blabla1 and vpc-blabla3 (Examples, could be any list of vpc-id)

I can get blabla1 with JMESpath filter

Vpcs[?VpcId=='blabla1'].OtherKey

But I can't find the syntax for multiple values? I have tried the Or syntax || and the composite syntax | but neither works? - See below for things I have tried.

 Vpcs[?VpcId=='blabla1' || 'blabla1'].OtherKey
 Vpcs[?VpcId=='blabla1' || ?VpcId=='blabla1'].OtherKey
 Vpcs[(?VpcId=='blabla1') || (?VpcId=='blabla1')].OtherKey
 Vpcs[?VpcId=='blabla1' | ?VpcId=='blabla1'].OtherKey

Any suggestions? Is this possible or am I going to have to gather one result set at a time and recombine the results I want?

Ourself answered 21/6, 2016 at 13:6 Comment(0)
I
59

The general syntax for multiple is [? expr1 || expr2] so in your case, you can use:

Vpcs[?VpcId=='vpc-blabla1' || VpcId=='vpc-blabla2'].OtherKey

Another option, if you have many VPC ids you're search for, you can also say:

Vpcs[?contains(`["vpc-blabla1", "vpc-blabla2"]`, VpcId)].OtherKey
Internist answered 21/6, 2016 at 16:6 Comment(2)
Excellent. I was so close, I just couldn't see the answer in the documentation.Ourself
doc link would be helpfulDiplosis
C
0

The full cli command in bash (for Azure, but it does not matter) would look like that:

az group list --subscription Platform_Sub --query "[?contains(['Enterprise Architecture', 'Platform Engineering', 'Product Engineering'], tags.Department)].{name:name, tags:tags}"
Cephalization answered 28/12, 2023 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.