I have the following json:
[
{"id": "1", "type": "folder", "title": "folder-1"},
{"id": "2", "type": "folder", "title": "folder-2"},
{"id": "3", "type": "item", "title": "item-1", "folder": "1"},
{"id": "4", "type": "item", "title": "item-2", "folder": "2"},
{"id": "5", "type": "item", "title": "item-3"}
]
Basically, I need to produce this output using jq, which is similar to the result of sql join:
[
{"type": "item", "title": "item-1", "folder": "folder-1"},
{"type": "item", "title": "item-2", "folder": "folder-2"},
{"type": "item", "title": "item-3"}
]
Any ideas?
Try this filter:
You'll have to break this out into steps.
Get the items and folders and for each, take the values you're interested in and assign it a key to associate with.
Group all by the key
Then combine the values that have keys (folders) and the value otherwise
Output:
Here is another solution which works by separating the data into two objects
$folders
and$items
and then constructing the desired result.If your version of jq has INDEX/2
this can be simplified to