How do I extract a key with jq based on its child values

前端 未结 1 747
灰色年华
灰色年华 2021-01-28 22:25

I\'m trying to process some JSON with jq. Specifically, I want a particular key, based on its child value. Example, given:

{
  \"foo\": {\"primary\": true, \"b         


        
相关标签:
1条回答
  • 2021-01-28 22:43

    You need to "split" your object into an array of entries, e.g.

    [
      {
        "key": "foo",
        "value": {
          "primary": true,
          "blah": "beep"
        }
      }
      //...
    ]
    

    Then you can filter with .value.primary and map the result with .key:

    to_entries | map(select(.value.primary)  | .key)
    

    Returns:

    [
      "foo"
    ]
    

    Or to get just the first item of the array: (Thanks @nbari)

    to_entries | map(select(.value.primary)  | .key)[0]
    
    0 讨论(0)
提交回复
热议问题