I have the following JSON, which has files and folders (reserved words) inside to indicate the which are files and folders.
the_name_of_the_folder_itself:
"files": [array_of_files_]
"folders": [objects of folders inside this dir]
Its easier to see the JSON itself:
{
"configfiles": {
"folders": {
"dir1": {
"files": [
"data.dat",
"data2.dat"
]
},
"dir2": {
"folders": {
"subffolder": {
"files": [
[]
]
},
"subffolder_2": {
"files": [
"other.dat"
]
}
},
"files": [
[]
]
}
},
"files": [
[]
],
"LoadBase": "barfoo IGNORE THIS"
}
}
How can I get all the paths and files from that JSON, that means to have an output array with the following elements?
configfiles/
configfiles/dir1/
configfiles/dir1/data.dat
configfiles/dir1/data2.dat
configfiles/dir2/
configfiles/dir2/subffolder/
configfiles/dir2/subffolder_2/
configfiles/dir2/subffolder_2/other.dat
This is my try so far:
function getPathAndFolder(folderPath)
{
foldersArray = Object.keys(folderPath)
foldersArray.forEach(function callback(folderName, index, array)
{
finalArray.push(folderName)
filesArray = folderPath[folderName].files
filesArray.forEach(function callback(fileName, index, array)
{
finalPath = folderName + "/" + fileName
});
// Call it again
//getPathAndFolder()
});
}
finalArray = []
getPathAndFolder(inputJSONobject)
Can someone give me a hand, please?
{folders: { configfiles: { /* ... */ } }?foldersbefore.