Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMESPath JSON filter with multiple matches

Tags:

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?

like image 532
Sam Avatar asked Jun 21 '16 13:06

Sam


1 Answers

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 
like image 128
jamesls Avatar answered Sep 18 '22 18:09

jamesls