-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (36 loc) · 1.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const jsonForm = document.querySelector("#jsonform");
const csvForm = document.querySelector("#csvform");
const bConvert = document.querySelector("#bConvert");
bConvert.addEventListener("click", (e) => {
let json;
let keys = [];
let values = [];
try {
json = JSON.parse(jsonForm.value);
} catch (error) {
console.log("Formato incorrecto", error);
}
if (Array.isArray(json)) {
json.forEach((item) => {
const nkeys = Object.keys(item);
if (keys.length === 0) {
keys = [...nkeys];
} else {
if (nkeys.length != keys.length) {
throw new Error("Number of keys are different");
} else {
console.log("Bien", nkeys);
}
}
const row = keys.map((k) => {
return item[k];
});
values.push([...row]);
});
values.unshift(keys);
const text = values.map((v) => v.join(",")).join("\n");
csvForm.value = text;
} else {
alert("Error, this is not JSON");
}
});