Using jq to fetch key value from json output

后端 未结 3 1062
暖寄归人
暖寄归人 2021-02-12 08:29

I have a file that looks as below:

{
  \"repositories\": [
   {
    \"id\": \"156c48fc-f208-43e8-a631-4d12deb89fa4\",
    \"namespace\": \"rhel12\",
    \"namesp         


        
3条回答
  •  Happy的楠姐
    2021-02-12 08:50

    You need to combine filters by means of | operator:

    $ jq -r '.[] | .[] | .name' test.json 
    rhel6.6
    rhel7
    

    The first .[] fetches repositories array. The next .[] fetches all the items of the repositories array. Finally, .name extracts properties from the array items(objects).

    Note, the first .[] works on object because it is a documented feature:

    .[]
        If you use the .[index] syntax, but omit the index entirely, it
        will return all of the elements of an array...
    
        You can also use this on an object, and it will return all the
        values of the object.
    

提交回复
热议问题