使用jq选择多个键,然后将它们返回到数组中

时间:2018-08-22 15:44:31

标签: 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"}]

我想要这个输出:

[{"Key":"app","Value":"amibuild"},{"Key":"sbu","Value":"IT"},{"Key":"app_env","Value":"dev"}]

我已经了解到了这一点:

.[] | select(.Key == "app"), select(.Key == "app_env"), select(.Key == "sbu")

但这会导致:

{"Key":"app","Value":"amibuild"}
{"Key":"sbu","Value":"IT"}
{"Key":"app_env","Value":"dev"}

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

1 个答案:

答案 0 :(得分:1)

您只需要将结果包装在[...]中即可:

[.[] | select(.Key == "app"), select(.Key == "app_env"), select(.Key == "sbu")]

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

[.[] | select(.Key == "app" or .Key == "app_env" or .Key == "sbu")]

或使用map函数:

map(select(.Key == "app" or .Key == "app_env" or .Key == "sbu"))