Can we convert the below example using jq for bash (https://stedolan.github.io/jq/)?
The requirement is to convert the file paths into json as given in the below example
const data = [
"/parent/child1/grandchild1"
"/parent/child1/grandchild2"
"/parent/child2/grandchild1"
];
const output = {};
let current;
for (const path of data) {
current = output;
for (const segment of path.split('/')) {
if (segment !== '') {
if (!(segment in current)) {
current[segment] = {};
}
current = current[segment];
}
}
}
console.log(output);
The following assumes:
reduce .[] as $entry ({};
($entry | split("/") ) as $names
| $names[1:-1] as $p
| setpath($p; getpath($p) + [$names[-1]]) )
[
"/parent/child1/grandchild1",
"/parent/child1/grandchild2",
"/parent/child2/grandchild3",
"/parent/child2/grandchild4",
"/parent2/child2/grandchild5"
]
{
"parent": {
"child1": [
"grandchild1",
"grandchild2"
],
"child2": [
"grandchild3",
"grandchild4"
]
},
"parent2": {
"child2": [
"grandchild5"
]
}
}