简体   繁体   English

如何使用 jq 根据键值对过滤 object?

[英]how to use jq to filter an object based on it's key:value pair?

Assuming this is the input假设这是输入

>echo '{"A": {"x": 1}, "B": {"x":2}, "C":{"x":3}}' | jq '.'
{
  "A": {
    "x": 1
  },
  "B": {
    "x": 2
  },
  "C": {
    "x": 3
  }
}

I would like to get the return object of "x" == 2 .我想获得"x" == 2的回报 object 。 The only way i know of how to achieve that is thru this我知道如何实现这一点的唯一方法就是通过这个

>echo '{"A": {"x": 1}, "B": {"x":2}, "C":{"x":3}}' | jq '.[] | select(.x==2)'
{
  "x": 2
}

Is there a way to have jq return me like this instead?有没有办法让 jq 像这样返回我?

{
  "B": {
    "x": 2
  },
}

 
with_entries(select(.value.x == 2))

You can also use map_values:您还可以使用 map_values:

map_values(select(.x == 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM