简体   繁体   English

如何使用 jq 获取 {“k”:foo, “v”:bar} 项的 JSON 列表中的键值?

[英]How to get value for key in JSON list of {“k”:foo, “v”:bar} items using jq?

Suppose I have this JSON:假设我有这个 JSON:

[
    {
        "k": foo,
        "v": 1
    },
    {
        "k": bar,
        "v": 2
    }
]

How to get the value for the "bar" key?如何获取"bar"键的值?

You can use select(expr) to find the object with a key-value "k":"bar" and then you can extract the value of the "v" key.您可以使用select(expr)找到具有键值"k":"bar"的 object,然后您可以提取"v"键的值。

$ jq . test.json 
[
  {
    "k": "foo",
    "v": 1
  },
  {
    "k": "bar",
    "v": 2
  }
]

$ jq -r '.[] | select(.k == "bar") | .v' test.json
2

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

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