I have (n) number of JSON files in a specific folder:
For simplicity lets assume 3 JSON files Animal.json, Noun.json, POS.JSON
and their contents respectively are
Animal.json
[
{
"label": "Dinosaur",
"sample": ["#Noun Rex","saurus"]
},
{
"label": "Lion",
"sample": ["simba"]
},
{
"label": "Tiger",
"sample": ["big cat"]
}
]
Noun.json
[
{
"label": "Animal",
"sample": ["Herbivore","Carnivore"]
}
]
POS.json
[
{
"label": "Noun",
"sample": ["Proper","Common"]
}
]
I want to be able to loop through all the JSON files in a specific folder and dynamically build a JSON in the below format
label: {
Dinosaur: {
isA: 'Animal'
},
Lion: {
isA: 'Animal'
},
Tiger: {
isA: 'Animal'
},
Animal: {
isA: 'Noun'
},
Noun: {
isA: 'POS'
}
},
sample: {
'#Noun rex|saurus': 'Dinosaur',
'simba': 'Lion'
'big cat': 'Tiger',
'Herbivore|Carnivore' : 'Animal',
'Proper|Common' : 'Noun'
}
Logic I have so far :
function buildJSON() {
fs.readdirSync('/path/to/file').forEach(file => {
const path = '/path/to/file' + file;
const data = fs.readFileSync(path);
const txt = JSON.parse(data);
console.log(JSON.stringify(txt)); //Displays content of each file
/* I need the logic to build the lable and sample for the output json */
});
}
Any help/ guidance is appreciated.