What is expected?:
I would like to convert the below listed directory structure
into a single JSON file.
The directory structure contains JSON files
that are supposed to be included in
the output file as well.
Limitations:
Node.js
Questions:
What would be a good/efficient
way to generate the desired output,
using Node.js + Modules?
Logically, what would be the
necessary steps?
Directory structure:
JavaScript
x
45
45
1
CMS/
2
├── en/
3
| ├──brand_one/
4
| | ├──footer.json
5
| | ├──header.json
6
| ├──brand_two/
7
| | ├──footer.json
8
| | ├──header.json
9
| ├──brand_three/
10
| | ├──footer.json
11
| | ├──header.json
12
├── de/
13
| ├──brand_one/
14
| | ├──footer.json
15
| | ├──header.json
16
| ├──brand_two/
17
| | ├──footer.json
18
| | ├──header.json
19
| ├──brand_three/
20
| | ├──footer.json
21
| | ├──header.json
22
├── fr/
23
| ├──brand_one/
24
| | ├──footer.json
25
| | ├──header.json
26
| ├──brand_two/
27
| | ├──footer.json
28
| | ├──header.json
29
| ├──brand_three/
30
| | ├──footer.json
31
| | ├──header.json
32
├── es/
33
| ├──brand_one/
34
| | ├──footer.json
35
| | ├──header.json
36
| ├──brand_two/
37
| | ├──footer.json
38
| | ├──header.json
39
| ├──brand_three/
40
| | ├──footer.json
41
| | ├──header.json
42
43
[ ]
44
45
Desired output:
JavaScript
1
40
40
1
// content.json
2
3
{
4
"en":[
5
{
6
"brand_one":{
7
"footer":{
8
"val": "value",
9
[ ]
10
},
11
"header":{
12
"val": "value",
13
[ ]
14
}
15
},
16
"brand_two":{
17
"footer":{
18
"val": "value",
19
[ ]
20
},
21
"header":{
22
"val": "value",
23
[ ]
24
}
25
},
26
"brand_three":{
27
"footer":{
28
"val": "value",
29
[ ]
30
},
31
"header":{
32
"val": "value",
33
[ ]
34
}
35
}
36
}
37
],
38
[ ]
39
}
40
Advertisement
Answer
You could create a function to convert a directory to an object, with a property for each dir / file.
This would then be called recursively to walk the entire tree, in this case using the fs/promises functions.
JavaScript
1
26
26
1
const fs = require('fs/promises');
2
const path = require('path');
3
4
async function walkDir(dir, result = {}) {
5
let list = await fs.readdir(dir);
6
for(let item of list) {
7
const itemPath = path.join(dir, item);
8
let stats = await fs.stat(itemPath)
9
if (await stats.isDirectory()) {
10
result[item] = {};
11
await walkDir(itemPath, result[item]);
12
} else {
13
const fileName = path.basename(item, path.extname(item));
14
result[fileName] = JSON.parse(await fs.readFile(itemPath, { encoding: 'utf-8'}));
15
}
16
}
17
return result;
18
}
19
20
async function testWalkDir() {
21
let result = await walkDir('./CMS')
22
console.log("Result:", JSON.stringify(result, null, 2));
23
}
24
25
testWalkDir();
26
Assuming each file looks like
JavaScript
1
4
1
{
2
"some_key": "some_val"
3
}
4
I get a result that looks like so:
JavaScript
1
31
31
1
{
2
"en": {
3
"brand_one": {
4
"footer": {
5
"some_key": "some_val"
6
},
7
"header": {
8
"some_key": "some_val"
9
}
10
},
11
"brand_three": {
12
"footer": {
13
"some_key": "some_val"
14
},
15
"header": {
16
"some_key": "some_val"
17
}
18
},
19
"brand_two": {
20
"footer": {
21
"some_key": "some_val"
22
},
23
"header": {
24
"some_key": "some_val"
25
}
26
}
27
}
28
}
29
30
31