-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
367 lines (310 loc) · 14.7 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Convert JSON data into a readable HTML table or download it as a CSV file with our easy-to-use JSON to HTML Converter.">
<meta name="keywords" content="JSON, HTML, converter, JSON to HTML, data visualization, CSV download">
<meta name="author" content="Erik Taveras">
<title>JSON to HTML Converter</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<style>
body { font-family: 'Montserrat', sans-serif; display: flex; flex-direction: column; min-height: 100vh; min-width:80%;}
#main-content { margin-left: 10%; margin-right: 10%; text-align: center;}
.ad-space { background-color: #f8f9fa; text-align: center; padding: 20px; margin: 15px 0; }
.ad-space p { margin: 0; }
.input-group-text { background-color: #ffffff; }
.header { padding: 20px 0; }
/* Dark mode styles */
body.dark-mode { background-color: #121212; color: #e0e0e0; }
.dark-mode .card { background-color: #1f1f1f; color: #e0e0e0; }
.dark-mode .ad-space { background-color: #121212; }
.dark-mode .header { background-color: #1f1f1f; color: #e0e0e0; }
.dark-mode .btn-success { background-color: #388e3c; border-color: #388e3c; }
.dark-mode .border-primary { border-color: #1e88e5 !important; }
.dark-mode .text-secondary { color: #b0bec5 !important; }
.dark-mode .table-dark { background-color: #1f1f1f; color: #e0e0e0; }
.dark-mode .table-dark th { border-color: #424242; }
.dark-mode .table-dark td { border-color: #424242; }
.dark-mode .table-dark thead th { border-color: #424242; }
.dark-mode .table-dark thead th { border-color: #424242; }
.dark-mode .text-sub { color: #b0bec5; }
body.dark-mode .modal-content {
background-color: #232323;
color: #e0e0e0;
}
body.dark-mode .modal-header {
border-bottom: 1px solid #444;
}
body.dark-mode .modal-footer {
border-top: 1px solid #444;
}
body.dark-mode .bg-light {
background-color: #232323 !important;
}
body.dark-mode .border {
border-color: #444 !important;
}
body.dark-mode .btn-secondary {
background-color: #333;
border-color: #444;
color: #e0e0e0;
}
body.dark-mode .btn-secondary:hover {
background-color: #444;
color: #fff;
}
</style>
</head>
<body>
<!-- Dark mode switch -->
<div class="text-end pe-3 pt-3">
<button id="darkModeSwitch" class="btn btn-outline-secondary py-2 px-3 mb-3">
<i id="darkModeIcon" class="fas fa-moon"></i>
</button>
</div>
<!-- Header -->
<header class="header text-center">
<h1 class="display-4"><i class="fas fa-code"></i> JSON to HTML Converter</h1>
<p id="headerText" class="lead text-muted"><i class="fas fa-info-circle"></i> Easily convert your JSON data into a user-friendly HTML table.</p>
</header>
<!-- Botones para exportar -->
<div class="d-flex justify-content-center flex-wrap" style=" margin-bottom: 30px;">
<button id="download_csv" class="btn btn-primary m-2 py-2 px-4">
<i class="fas fa-file-csv fa-lg"></i> Download CSV
</button>
<button id="download_xlsx" class="btn btn-info m-2 py-2 px-4">
<i class="fas fa-file-excel fa-lg"></i> Download Excel
</button>
<button id="generate_sql" class="btn btn-warning m-2 py-2 px-4">
<i class="fas fa-database fa-lg"></i> Generate SQL
</button>
<button id="copy_html" class="btn btn-success m-2 py-2 px-4">
<i class="fas fa-copy fa-lg"></i> Copy HTML
</button>
<script>
</script>
</div>
<!-- Modal -->
<div class="modal fade" id="htmlModal" tabindex="-1" aria-labelledby="htmlModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="htmlModalLabel"><i class="fas fa-table"></i> HTML Table Preview</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body py-4">
<pre><code id="html_preview" class="border p-2"></code></pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal"><i class="fas fa-times"></i> Close</button>
</div>
</div>
</div>
</div>
<!-- SQL Result Modal -->
<div class="modal fade" id="sqlModal" tabindex="-1" aria-labelledby="sqlModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="sqlModalLabel">Generated SQL Query</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<pre id="sqlOutput" class="bg-light p-3 border rounded" style="white-space: pre-wrap;"></pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<main id="main-content" class="px-2">
<div class="row g-3">
<div class="col-lg-4 col-md-6 mb-3">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title"><i class="fas fa-paste"></i> JSON Input</h5>
<div class="input-group input-group-sm mb-3">
<textarea id="json_input" class="form-control" aria-label="JSON input" rows="10" placeholder="Paste your JSON here..."></textarea>
</div>
<button id="convert_button" class="btn btn-success"><i class="fas fa-sync-alt"></i> Convert</button>
</div>
</div>
</div>
<div class="col-lg-8 col-md-6">
<div class="card h-100 border-primary">
<div class="card-body">
<h5 class="card-title"><i class="fas fa-table"></i> HTML Table</h5>
<div id="json_result" class="border rounded p-2" style="height: 400px; overflow-y: scroll;">
<p class="text-secondary">Your HTML table will appear here...</p>
</div>
</div>
</div>
</div>
</div>
</main>
<!-- Footer -->
<footer class="footer mt-auto py-2 bg-dark text-light">
<div class="container text-center">
<p class="small mb-1">Made with <i class="fas fa-heart"></i> by Erik Taveras</p>
<p class="small">
<a href="https://github.com/eriktaveras" class="text-decoration-none text-light me-3" target="_blank">
<i class="fab fa-github"></i> Check it out on GitHub
</a>
<a href="https://www.linkedin.com/in/eriktaveras" class="text-decoration-none text-light me-3" target="_blank">
<i class="fab fa-linkedin-in"></i> Connect on LinkedIn
</a>
</p>
</div>
</footer>
<!-- Scripts for Export Functionality -->
<script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js"></script>
<script>
document.getElementById('download_xlsx').addEventListener('click', function() {
var data = document.getElementById('json_result').querySelector('table');
if (data) {
var wb = XLSX.utils.table_to_book(data);
XLSX.writeFile(wb, 'data.xlsx');
} else {
alert('No data to download');
}
});
document.getElementById('generate_sql').addEventListener('click', function() {
try {
var data = JSON.parse(document.getElementById('json_input').value);
if (!Array.isArray(data) || data.some(item => typeof item !== 'object' || Array.isArray(item))) {
throw new Error("Please provide a JSON array of objects.");
}
var tableName = "your_table_name"; // Customize as needed
var sql = `INSERT INTO ${tableName} `;
var keys = Object.keys(data[0]);
sql += `(${keys.join(', ')}) VALUES \n`;
data.forEach((row, index) => {
var values = keys.map(key => `'${JSON.stringify(row[key]).slice(1, -1)}'`);
sql += `(${values.join(', ')})`;
if (index < data.length - 1) sql += ',\n';
});
sql += ';';
// Actualizar y mostrar el modal
document.getElementById('sqlOutput').textContent = sql;
var sqlModal = new bootstrap.Modal(document.getElementById('sqlModal'));
sqlModal.show();
} catch (e) {
console.error("Error parsing JSON: ", e.message);
alert("Error parsing JSON: " + e.message);
}
});
document.getElementById('download_csv').addEventListener('click', function() {
var data = document.getElementById('json_result').querySelector('table');
if (!data) {
alert('No data to download');
return;
}
var csv = [];
var rows = data.querySelectorAll("tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push('"' + cols[j].innerText + '"');
csv.push(row.join(","));
}
// Download CSV file
downloadCSV(csv.join("\n"));
});
function downloadCSV(csv) {
var csvFile;
var downloadLink;
// CSV file
csvFile = new Blob([csv], {type: "text/csv"});
// Download link
downloadLink = document.createElement("a");
// File name
downloadLink.download = 'data.csv';
// Create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);
// Hide download link
downloadLink.style.display = "none";
// Add the link to DOM
document.body.appendChild(downloadLink);
// Click download link
downloadLink.click();
}
</script>
<script>
document.getElementById('darkModeSwitch').addEventListener('click', function() {
document.body.classList.toggle('dark-mode');
var icon = document.getElementById('darkModeIcon');
icon.classList.toggle('fa-moon');
icon.classList.toggle('fa-sun');
this.classList.toggle('btn-outline-light');
});
</script>
<script>
document.getElementById('convert_button').addEventListener('click', function() {
const input = document.getElementById('json_input').value;
let data;
try {
data = JSON.parse(input);
} catch(e) {
document.getElementById('json_result').innerHTML = `<p>Invalid JSON: ${e}</p>`;
return;
}
let valuesList = getValuesList(data);
if (valuesList.length) {
const keys = Object.keys(valuesList[0]);
const resultTable = generateTable(keys, valuesList);
document.getElementById('json_result').innerHTML = resultTable;
} else {
document.getElementById('json_result').innerHTML = '<p>Cannot display data as a table.</p>';
}
});
function getValuesList(data) {
if (typeof data === 'object' && !Array.isArray(data)) {
if (Object.keys(data).length === 1 && Array.isArray(Object.values(data)[0])) {
return Object.values(data)[0];
} else {
return [data];
}
} else if (Array.isArray(data)) {
return data;
} else {
return [];
}
}
function generateTable(keys, valuesList) {
let table = '<table class="table table-bordered"><thead class="table-dark"><tr>';
keys.forEach(key => {
table += `<th>${key}</th>`;
});
table += '</tr></thead><tbody>';
valuesList.forEach(item => {
table += '<tr>';
keys.forEach(key => {
table += `<td>${item[key]}</td>`;
});
table += '</tr>';
});
table += '</tbody></table>';
return table;
}
document.getElementById('copy_html').addEventListener('click', function() {
const html = document.getElementById('json_result').innerHTML;
const copyButton = document.getElementById('copy_html');
navigator.clipboard.writeText(html).then(function() {
copyButton.textContent = 'Copied!';
setTimeout(function() {
copyButton.textContent = 'Copy HTML';
}, 2000);
}, function(err) {
console.error('Could not copy text: ', err);
});
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>