Reputation: 177
I have a JSON of an array of key - value pairs and I want to convert it to a CSV file of two columns. All resources I tried online converted it to 2 rows of many columns, but I want two columns of multiple rows.
Example, what I have:
{
"abnormal": "Abnormal",
"action_bar": "Action bar",
"active": "Active"
}
What I want:
| abnormal | Abnormal |
| action_bar| Action bar|
| active | Active |
Upvotes: 0
Views: 166
Reputation: 997
Which programming language are you using?
If it's python, I recommend pandas
If it's javascript then here:
var data = {
"abnormal": "Abnormal",
"action_bar": "Action bar",
"active": "Active"
}
var csv = "";
var replacer = []
for (const key in data) {
csv += key + "," + data[key] + "\r\n";
}
console.log(csv)
var exportedFilenmae = 'export.csv';
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, exportedFilenmae);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", exportedFilenmae);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
Upvotes: 1