使用 jq,选择多个键并在数组中返回它们

DrS*_*ork 1 jq

鉴于此数组:

[{"Key":"base_ami","Value":"ami-46d003ac"},
{"Key":"app","Value":"amibuild"},
{"Key":"sbu","Value":"IT"},
{"Key":"base_ami_image_location","Value":"123456789012/amazon-linux"},
{"Key":"app_env","Value":"dev"},
{"Key":"Name","Value":"amazon-linux"},
{"Key":"jenkins_build_id","Value":"24"},
{"Key":"os_type","Value":"linux"},
{"Key":"version","Value":"1.0.24"}]
Run Code Online (Sandbox Code Playgroud)

我想要这个输出:

[{"Key":"app","Value":"amibuild"},{"Key":"sbu","Value":"IT"},{"Key":"app_env","Value":"dev"}]
Run Code Online (Sandbox Code Playgroud)

我已经把它弄下来了:

.[] | select(.Key == "app"), select(.Key == "app_env"), select(.Key == "sbu")
Run Code Online (Sandbox Code Playgroud)

但这会导致:

{"Key":"app","Value":"amibuild"}
{"Key":"sbu","Value":"IT"}
{"Key":"app_env","Value":"dev"}
Run Code Online (Sandbox Code Playgroud)

我需要那些作为数组元素返回的单个对象。

che*_*ner 5

您只需要将结果包装在[...]

[.[] | select(.Key == "app"), select(.Key == "app_env"), select(.Key == "sbu")]
Run Code Online (Sandbox Code Playgroud)

您还可以稍微缩短此过滤器:

[.[] | select(.Key == "app" or .Key == "app_env" or .Key == "sbu")]
Run Code Online (Sandbox Code Playgroud)

或者使用map函数:

map(select(.Key == "app" or .Key == "app_env" or .Key == "sbu"))
Run Code Online (Sandbox Code Playgroud)