I'm unsure if "transpose" is the correct term here, but I'm looking to use jq
to transpose a 2-dimensional object such as this:
[
{
"name": "A",
"keys": ["k1", "k2", "k3"]
},
{
"name": "B",
"keys": ["k2", "k3", "k4"]
}
]
I'd like to transform it to:
{
"k1": ["A"],
"k2": ["A", "B"],
"k3": ["A", "B"],
"k4": ["A"],
}
I can split out the object with .[] | {key: .keys[], name}
to get a list of keys and names, or I could use .[] | {(.keys[]): [.name]}
to get a collection of key–value pairs {"k1": ["A"]}
and so on, but I'm unsure of the final concatenation step for either approach.
Are either of these approaches heading in the right direction? Is there a better way?
group_by
, but I have to admit the nestedmap
following it is still throwing me a little. Is there a simpler example or documentation of that behaviour? – Calle