JMESPath JSON filter with multiple matches

Sam picture Sam · Jun 21, 2016 · Viewed 7.2k times · Source

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?

Answer

jamesls picture jamesls · Jun 21, 2016

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