I need to build a application to extract data from multiple JSON-Files and store it into a Database.
The File looks like this and parsed results in a highly nested object.
To explain, i need to go through all Items in the "Mandanten" Object and for each key i need to go into the "Saison" Objects and get again a list of key value pairs, which is needed to go into the "CompetitionTypes" Object and there i set the "Mandanten"-key as well as the "Saison"-key and get another list of key value pairs. All these keys are now needed to download another file and do a similar process again.
My solution to extract the data resulted in a triple nested loop and very ineffective code, which is shown below
I implemented a solution which was coded very fast and is working, but it looks very ugly for me. I want to refactor this now to a better and more important faster, because when i run it the last time it needed 25min to run.
Is there a way to avoid these triple nested loop or any other ways to make it faster?
function loadKinds() {
let wam = undefined;
try {
wam = JSON.parse(wamString);
} catch (error) {
console.log(error);
}
console.log(wam)
const mandanten = wam.Mandanten;
const saisons = wam.Saisons;
const competitons = wam.CompetitionTypes;
const mandanten_map = new Map();
const saisons_map = new Map();
const competitonsTypes_map = new Map();
const combinations = [];
Object.keys(mandanten).forEach(key => {
mandanten_map.set(formatKey(key), mandanten[key]);
const formatted_mandant = formatKey(key);
Object.keys(saisons[formatted_mandant]).forEach(key => {
saisons_map.set(formatKey(key), saisons[formatted_mandant][key]);
const formatted_saison = formatKey(key);
Object.keys(competitons[formatted_mandant][formatted_saison]).forEach(key => {
competitonsTypes_map.set(formatKey(key), competitons[formatted_mandant][formatted_saison][key]);
combinations.push([formatted_mandant, formatted_saison, formatKey(key)]);
});
});
});
return ({
mandanten: Array.from(mandanten_map),
saisons: Array.from(saisons_map),
competition_types: Array.from(competitonsTypes_map),
competitions: combinations
});
}
function formatKey(key) {
return key.replace('_', '');
}
loadKinds();
<script>
const wamString = `{
"currentSaison": "2223",
"defaultCompetitionType": "1",
"Mandanten": {
"_21": "Westfalen",
"_22": "Niederrhein",
"_23": "Mittelrhein",
"_31": "Bayern"
},
"Saisons": {
"21": {
"_2324": "23/24",
"_2223": "22/23",
"_2122": "21/22"
},
"22": {
"_2223": "22/23",
"_2122": "21/22",
"_2021": "20/21"
},
"23": {
"_2223": "22/23",
"_2122": "21/22",
"_2021": "20/21"
},
"31": {
"_2223": "22/23",
"_2122": "21/22",
"_2021": "20/21"
}
},
"CompetitionTypes": {
"21": {
"2122": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb"
},
"2223": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
},
"2324": {
"_300": "Turniere"
}
},
"22": {
"2021": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb"
},
"2122": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb"
},
"2223": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
}
},
"23": {
"2021": {
"_1": "Meisterschaften",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb"
},
"2122": {
"_1": "Meisterschaften",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb"
},
"2223": {
"_1": "Meisterschaften",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
},
"31": {
"2021": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
},
"2122": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
},
"2223": {
"_1": "Meisterschaften",
"_305": "Spielnachmittage",
"_308": "Pokale",
"_300": "Turniere",
"_70": "Freundschaftsspiele",
"_11": "Futsal-Ligabetrieb",
"_2": "Hallenturniere"
}
}
}
}
}
`
</script>
