-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-to-csv.html
32 lines (31 loc) · 1.05 KB
/
json-to-csv.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Tools - JWT</title>
<script src="components/sidebar-component.js"></script>
<script src="components/header-component.js"></script>
<link href="index.css" rel="stylesheet">
</head>
<body>
<header-component></header-component>
<sidebar-component></sidebar-component>
<main>
<textarea id="content"></textarea>
<code id="output"></code>
<button onclick="jsonToCsv()">Parse</button>
</main>
<script>
function jsonToCsv() {
console.log(document.getElementById('content').value);
const items = JSON.parse(document.getElementById('content').value.trim());
const replacer = (key, value) => value === null ? '' : value; // specify how you want to handle null values here
const header = Object.keys(items[0]);
let csv = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','));
csv.unshift(header.join(','));
csv = csv.join('\r\n');
document.getElementById('output').innerHTML = csv;
}
</script>
</body>
</html>