mogul05
mogul05

Reputation: 1

filter specific entries in an array using jq

i'll make this short. This is what im dealin with. I've got a link using curl to pipe a json format output to filter specific entries using jq. This is the json file

{
    "remote":  [
        {"name": "cast-1",
         "id": 1212
        },
        {"name": "cast-1",
         "id": 1214
        },
        {"name": "home-11",
         "id": 3212
        },
        {"name": "cast-3",
         "id": 3212
        },
        {"name": "cast-3",
         "id": 3213
        },
        {"name": "cast-4",
         "id": 4211
        }

    ]
}

my desired output is

 "cast-1": 1212 , 1214,
 "cast-3": 3212 , 3213,
 "cast-4": 4211,

with my attempt, i only am able to output this

 "cast-1": 1214,
 "cast-3": 3213, 
 "cast-4": 4211,

my code so far

curl ... | jq  'reduce .remote[] as $v ({}; . + {"\($v.name)":$v.id})'  

Cant figure out to add the second id from cast-1 and cast-3. Is there a method to format the output, to get a valid JSON output format? maybe python or perl method? Im not familiar with awk, sed to let you know.

Upvotes: 0

Views: 120

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

You don't need reduce here, group_by should suffice. E.g:

.remote | group_by(.name) | map({(.[0].name): map(.id)}) | add

https://jqplay.org/s/_0CFADWu2G

Upvotes: 1

Related Questions