简体   繁体   中英

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

Suppose I have this JSON:

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

How to get the value for the "bar" key?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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