I have this list of paths, or locations, that represents the contents of an aws s3 bucket:
const keysS3 = [
'platform-tests/',
'platform-tests/datasets/',
'platform-tests/datasets/random_csv_datasets/',
'platform-tests/datasets/random_csv_datasets/1/',
'platform-tests/datasets/random_csv_datasets/1/raw/',
'platform-tests/datasets/random_csv_datasets/1/raw/change_name_dtype.json',
'platform-tests/datasets/random_csv_datasets/1/raw/change_name_dtype_2.csv',
'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack.json',
'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_0.csv',
'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_1.csv',
'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_2.csv',
'platform-tests/test-folder/',
'README.md',
'preprocessed_data.csv',
'preprocessed_data2 copy.csv',
'reg1.csv',
'tested_demo.csv'
];
and I'm writing this code in Typescrtip:
let output = {};
let current: any;
for (const path of keysS3) {
current = output;
const segment = path.split('/');
segment.forEach((value, index) => {
if (value !== ''&& index + 1 < segment.length) {
if (!(value in current)) {
current[value] = {};
}
current = current[value];
} else {
current[value] = null;
}
});
}
console.log(JSON.stringify(output));
to convert it to this schema, here is the output:
{
"platform-tests":{
"datasets":{
"random_csv_datasets":{
"1":{
"raw":{
"change_name_dtype.json":null,
"change_name_dtype_2.csv":null,
"random_data_stack.json":null,
"random_data_stack_0.csv":null,
"random_data_stack_1.csv":null,
"random_data_stack_2.csv":null
}
}
}
},
"test-folder":{
}
},
"README.md":null,
"preprocessed_data.csv":null,
"preprocessed_data2 copy.csv":null,
"reg1.csv":null,
"tested_demo.csv":null
}
but, the issue is that I'm getting this schema, which it has this extra field that has been added after each sub-folder
"":null
{
"platform-tests":{
"":null,
"datasets":{
"":null,
"random_csv_datasets":{
"1":{
"":null,
"raw":{
"":null,
"change_name_dtype.json":null,
"change_name_dtype_2.csv":null,
"random_data_stack.json":null,
"random_data_stack_0.csv":null,
"random_data_stack_1.csv":null,
"random_data_stack_2.csv":null
}
},
"":null
}
},
"test-folder":{
"":null
}
},
"README.md":null,
"preprocessed_data.csv":null,
"preprocessed_data2 copy.csv":null,
"reg1.csv":null,
"tested_demo.csv":null
}