0

I'd like to aggregate a list of objects like this:

[ {"a": 1, "b": 2}, {"a": 3, "b": 4} ] 

To a single object with list values:

{ "a": [1,3], "b": [2,4] } 

I've scoured the docs but I think I just don't know the term for what I'm trying to do. Any ideas?

2 Answers 2

1

A straightforward and efficient approach would be:

reduce .[] as $o ({}; reduce ($o|keys_unsorted[]) as $k (.; .[$k] += [$o[$k]])) 
Sign up to request clarification or add additional context in comments.

Comments

1

Here is another approach using reduce and to_entries:

reduce ( .[] | to_entries[] ) as {$key,$value} ({}; .[$key] += [$value]) 

Try it online!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.