jq - How do I print a parent value of an object when I am already deep into the object's children?

后端 未结 3 1907
生来不讨喜
生来不讨喜 2020-12-15 03:37

Say I have the following JSON, stored in my variable jsonVariable.

{
    \"id\": 1,
    \"details\": {
        \"username\": \"jamesbrown\",
                


        
3条回答
  •  攒了一身酷
    2020-12-15 04:02

    Rather than querying up to the value you're testing for, query up to the root object that contains the value you're querying on and the values you wish to select.

    You need the object that contains both the id and the name.

    $ jq --arg name 'James Brown' 'select(.details.name == $name).id' input.json
    

提交回复
热议问题