Disclaimer: I'm not very much experienced with JavaScript, so please excuse me if I srewed up anywhere. Anyways I want to share my suggestion.
In the first step, I would build a function, which can read a JSON file from your harddisk. For that I am using the function by the user @Stano, which he suggested in this answer.
function readFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200")
{
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
You can now read a JSON file from your harddisk, like down below.
readFile("test.json", function(text) {
var data = JSON.parse(text);
console.log(data);
});
Let's take a look at an example by using the following JSON structure.
{
"status": "Success",
"data": {
"key": [
{
"more_keys": "Some value"
}
]
}
}
You can extract the values from the above JSON structure like this:
readFile("my_test_json.json", function(text){
var data = JSON.parse(text);
console.log(data.status); // => Success
console.log(data.key[0].more_keys); // => Some value
});
With that being said you can now iterate through your files and merch the JSON files into a two-dimensional array. Why?. Because it's very easy to serialize a .csv
file from it. For that I am using the following approach, which was suggested by the user @Default in this answer.
const rows = [
["name1", "city1", "some other info"],
["name2", "city2", "more info"]
];
let csvContent = "data:text/csv;charset=utf-8,";
rows.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});
Then you can use JavaScript's window.open
and encodeURI
functions
to download the CSV file like so:
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
I hope that I could give you an idea how you can approach it, cheers!