How to extract all (also nested) key names with jq

前端 未结 1 587
情话喂你
情话喂你 2021-02-02 17:23

How can I extract all key names, even in nested objects with jq? For example, I have json:

{ \"a\": 1, \"b\": { \"c\": 2 } }

and I wa

1条回答
  •  醉酒成梦
    2021-02-02 17:30

    Short jq solution:

    jq -r '[paths | join(".")]'  jsonfile
    

    The output:

    [
      "a",
      "b",
      "b.c"
    ]
    

    • paths function outputs the paths to all the elements in its input

    • join(".") - to concatenate keys within hierarchical paths

    0 讨论(0)
提交回复
热议问题