Repository navigation
Expand file tree
/
Copy pathjson-to-csv.html
More file actions
928 lines (842 loc) · 27.1 KB
/
Copy pathjson-to-csv.html
File metadata and controls
928 lines (842 loc) · 27.1 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JSON to CSV Converter</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family:
Helvetica,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Arial,
sans-serif;
line-height: 1.6;
max-width: 980px;
margin: 0 auto;
padding: 20px;
color: #24292e;
background: #ffffff;
}
h1 {
border-bottom: 1px solid #eaecef;
padding-bottom: 0.3em;
margin-top: 0;
font-size: 32px;
font-weight: 600;
}
.controls {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 16px;
flex-wrap: wrap;
justify-content: space-between;
}
input[type="file"] {
font-size: 16px;
font-family:
Helvetica,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Arial,
sans-serif;
color: transparent;
width: auto;
display: none;
}
input[type="file"]::file-selector-button {
margin-right: 0;
padding: 6px 12px;
background: #fafbfc;
color: #24292e;
border: 1px solid rgba(27, 31, 35, 0.15);
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-family:
Helvetica,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Arial,
sans-serif;
}
input[type="file"]::file-selector-button:hover {
background: #f3f4f6;
}
.btn {
background: #fafbfc;
color: #24292e;
padding: 8px 12px;
border-radius: 6px;
border: 1px solid rgba(27, 31, 35, 0.15);
cursor: pointer;
font-size: 14px;
line-height: 20px;
text-decoration: none;
font-family:
Helvetica,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Arial,
sans-serif;
}
.btn:hover {
background: #f3f4f6;
}
.btn.primary {
background: #0366d6;
color: #fff;
border: 1px solid rgba(27, 31, 35, 0.15);
}
.btn.primary:hover {
background: #0256c5;
}
.btn.secondary {
background: #fafbfc;
color: #24292e;
border: 1px solid rgba(27, 31, 35, 0.15);
}
.btn.secondary:hover {
background: #f3f4f6;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn.secondary:disabled:hover {
background: #fafbfc;
}
.btn.primary:disabled:hover {
background: #0366d6;
}
.panel {
background: #f6f8fa;
padding: 16px;
border-radius: 6px;
border: 1px solid #e1e4e8;
margin-bottom: 16px;
}
.panel h2 {
margin-top: 0;
margin-bottom: 12px;
font-size: 20px;
font-weight: 600;
color: #24292e;
cursor: pointer;
user-select: none;
display: flex;
align-items: center;
}
.panel h2::before {
content: "▼";
display: inline-block;
margin-right: 8px;
transition: transform 0.2s;
font-size: 12px;
}
.panel h2.collapsed::before {
transform: rotate(-90deg);
}
.panel-content {
transition: max-height 0.3s ease;
overflow: hidden;
}
.panel-content.collapsed {
display: none;
}
.table-wrapper {
background: #ffffff;
overflow-x: auto;
border-radius: 6px;
border: 1px solid #e1e4e8;
margin-bottom: 12px;
max-height: 400px;
overflow-y: auto;
}
.data-table {
width: 100%;
border-collapse: collapse;
font-size: 14px;
}
.data-table th {
background: #0366d6;
padding: 10px 12px;
text-align: left;
font-weight: 600;
color: #ffffff;
border: 1px solid #0366d6;
position: sticky;
top: 0;
z-index: 1;
}
.data-table td {
padding: 8px 12px;
border: 1px solid #e1e4e8;
color: #24292e;
text-align: left;
vertical-align: top;
}
.data-table tr:hover {
background: #f6f8fa;
cursor: pointer;
}
.json-output {
background: #ffffff;
padding: 12px;
overflow-x: auto;
border-radius: 6px;
border: 1px solid #e1e4e8;
font-family:
"SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
font-size: 14px;
color: #24292e;
margin: 0 0 12px 0;
min-height: 250px;
white-space: pre-wrap;
word-wrap: break-word;
}
.actions {
display: flex;
gap: 8px;
margin-bottom: 12px;
flex-wrap: wrap;
}
label.btn {
display: inline-block;
margin: 0;
}
.back-link {
display: inline-block;
margin-bottom: 16px;
color: #0366d6;
text-decoration: none;
font-size: 14px;
}
.back-link:hover {
text-decoration: underline;
}
.info {
font-size: 13px;
color: #586069;
margin-bottom: 12px;
}
.empty-state {
color: #586069;
font-style: italic;
}
.options {
background: #ffffff;
padding: 12px;
border-radius: 6px;
border: 1px solid #e1e4e8;
margin-bottom: 12px;
}
.option-group {
margin-bottom: 12px;
}
.option-group:last-child {
margin-bottom: 0;
}
.option-group label {
display: block;
margin-bottom: 6px;
font-size: 14px;
font-weight: 600;
color: #24292e;
}
.option-group select {
width: 100%;
padding: 6px 12px;
font-size: 16px;
font-family:
Helvetica,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Arial,
sans-serif;
border: 1px solid #e1e4e8;
border-radius: 6px;
background: #ffffff;
color: #24292e;
}
.option-group select:focus {
outline: none;
border-color: #0366d6;
}
.option-description {
font-size: 12px;
color: #586069;
margin-top: 4px;
}
#csvPanel:fullscreen {
border-radius: 0;
border: none;
margin: 0;
padding: 16px;
display: flex;
flex-direction: column;
height: 100%;
box-sizing: border-box;
overflow: hidden;
}
#csvPanel:fullscreen .panel-content {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
}
#csvPanel:fullscreen .table-wrapper {
flex: 1;
max-height: none;
height: auto;
resize: none;
overflow-x: auto;
overflow-y: auto;
min-height: 0;
}
</style>
</head>
<body>
<a href="index.html" class="back-link">← Back to Browser Tools</a>
<h1>JSON to CSV Converter</h1>
<div class="controls">
<div>
<label for="fileInput" class="btn primary">Load JSON</label>
<input
type="file"
id="fileInput"
accept=".json,.jsonl,.ndjson,application/json,application/x-ndjson"
/>
</div>
<button class="btn secondary" id="clearBtn">Clear</button>
</div>
<div class="panel" id="optionsPanel">
<h2 id="optionsHeader">Conversion Options</h2>
<div class="panel-content" id="optionsContent">
<div class="options">
<div class="option-group" id="arrayPathGroup" style="display: none">
<label for="arrayPath">Select Data to Convert</label>
<select id="arrayPath">
<option value="">-- Select data array --</option>
</select>
<div class="option-description">
Your JSON contains multiple data structures. Select which one to
convert to CSV.
</div>
</div>
<div class="option-group">
<label for="nestedHandling">Nested Object Handling</label>
<select id="nestedHandling">
<option value="flatten">
Flatten with dot notation (e.g., user.name)
</option>
<option value="stringify">Convert to JSON strings</option>
<option value="flattenDepth">Flatten with depth limit</option>
</select>
<div class="option-description">
Choose how to handle nested objects and arrays in the JSON data
</div>
</div>
<div class="option-group" id="depthLimitGroup" style="display: none">
<label for="depthLimit">Maximum Flattening Depth</label>
<select id="depthLimit">
<option value="1">1 level</option>
<option value="2" selected>2 levels</option>
<option value="3">3 levels</option>
<option value="4">4 levels</option>
<option value="5">5 levels</option>
</select>
<div class="option-description">
Limit how many levels deep to flatten. Deeper objects will be
converted to JSON strings.
</div>
</div>
</div>
</div>
</div>
<div class="panel" id="csvPanel">
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:12px;">
<h2 id="csvHeader" style="margin-bottom:0">CSV Data Preview</h2>
<button class="btn secondary" id="tableFullscreenBtn">Fullscreen</button>
</div>
<div class="panel-content" id="csvContent">
<div class="info">
Upload a JSON, JSONL, or NDJSON file to preview its contents and
convert to CSV format. Supports JSON arrays, single JSON objects, and
JSON Lines / NDJSON format (.jsonl, .ndjson, one object per line).
</div>
<div class="info" id="columnCount" style="display: none"></div>
<div class="table-wrapper">
<div id="tableDisplay" class="empty-state">No data loaded</div>
</div>
</div>
</div>
<div class="panel" id="outputPanel">
<h2 id="outputHeader">CSV Output</h2>
<div class="panel-content" id="outputContent">
<div class="actions">
<button class="btn primary" id="downloadBtn" disabled>
Download CSV
</button>
<button class="btn primary" id="copyBtn" disabled>
Copy to Clipboard
</button>
</div>
<div class="json-output" id="csvOutput">
<span class="empty-state">No data converted yet</span>
</div>
</div>
</div>
<script type="module">
const fileInput = document.getElementById("fileInput");
const optionsHeader = document.getElementById("optionsHeader");
const optionsContent = document.getElementById("optionsContent");
const csvHeader = document.getElementById("csvHeader");
const csvContent = document.getElementById("csvContent");
const outputHeader = document.getElementById("outputHeader");
const outputContent = document.getElementById("outputContent");
const tableDisplay = document.getElementById("tableDisplay");
const csvOutput = document.getElementById("csvOutput");
const downloadBtn = document.getElementById("downloadBtn");
const copyBtn = document.getElementById("copyBtn");
const clearBtn = document.getElementById("clearBtn");
const nestedHandling = document.getElementById("nestedHandling");
const arrayPath = document.getElementById("arrayPath");
const arrayPathGroup = document.getElementById("arrayPathGroup");
const depthLimit = document.getElementById("depthLimit");
const depthLimitGroup = document.getElementById("depthLimitGroup");
const columnCount = document.getElementById("columnCount");
let currentData = null;
let csvText = null;
let rawJsonData = null;
let detectedPaths = [];
// Toggle collapsible sections
optionsHeader.addEventListener("click", () => {
optionsHeader.classList.toggle("collapsed");
optionsContent.classList.toggle("collapsed");
});
csvHeader.addEventListener("click", () => {
csvHeader.classList.toggle("collapsed");
csvContent.classList.toggle("collapsed");
});
outputHeader.addEventListener("click", () => {
outputHeader.classList.toggle("collapsed");
outputContent.classList.toggle("collapsed");
});
// Flatten nested objects
function flattenObject(
obj,
prefix = "",
maxDepth = Infinity,
currentDepth = 0,
) {
const flattened = {};
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const value = obj[key];
const newKey = prefix ? `${prefix}.${key}` : key;
if (value === null || value === undefined) {
flattened[newKey] = "";
} else if (
typeof value === "object" &&
!Array.isArray(value) &&
currentDepth < maxDepth
) {
Object.assign(
flattened,
flattenObject(value, newKey, maxDepth, currentDepth + 1),
);
} else if (Array.isArray(value)) {
flattened[newKey] = JSON.stringify(value);
} else if (typeof value === "object") {
// Max depth reached, stringify the object
flattened[newKey] = JSON.stringify(value);
} else {
flattened[newKey] = value;
}
}
return flattened;
}
// Stringify nested objects
function stringifyNested(obj) {
const result = {};
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const value = obj[key];
if (value === null || value === undefined) {
result[key] = "";
} else if (typeof value === "object") {
result[key] = JSON.stringify(value);
} else {
result[key] = value;
}
}
return result;
}
// Process JSON data based on nested handling option
function processJSON(data, handling, maxDepth = Infinity) {
if (!Array.isArray(data)) {
data = [data];
}
switch (handling) {
case "flatten":
return data.map((item) => flattenObject(item));
case "stringify":
return data.map((item) => stringifyNested(item));
case "flattenDepth":
return data.map((item) => flattenObject(item, "", maxDepth));
default:
return data;
}
}
// Convert JSON to CSV
function jsonToCSV(data) {
if (!data || data.length === 0) return "";
// Get all unique keys
const keys = new Set();
data.forEach((obj) => {
Object.keys(obj).forEach((key) => keys.add(key));
});
const headers = Array.from(keys);
// Escape CSV field
function escapeCSVField(field) {
if (field === null || field === undefined) return "";
const str = String(field);
if (
str.includes('"') ||
str.includes(",") ||
str.includes("\n") ||
str.includes("\r")
) {
return '"' + str.replace(/"/g, '""') + '"';
}
return str;
}
// Build CSV
const csvLines = [];
csvLines.push(headers.map(escapeCSVField).join(","));
data.forEach((obj) => {
const row = headers.map((header) => {
const value = obj[header];
return escapeCSVField(value);
});
csvLines.push(row.join(","));
});
return csvLines.join("\n");
}
// Display data as table
function displayTable(data) {
if (!data || data.length === 0) {
tableDisplay.innerHTML =
'<span class="empty-state">No data in JSON</span>';
columnCount.style.display = "none";
return;
}
// Get all unique keys
const keys = new Set();
data.forEach((obj) => {
Object.keys(obj).forEach((key) => keys.add(key));
});
const headers = Array.from(keys);
// Update column count indicator
const rowCount = data.length;
const colCount = headers.length;
columnCount.textContent = `${colCount} column${
colCount !== 1 ? "s" : ""
} × ${rowCount} row${rowCount !== 1 ? "s" : ""}`;
columnCount.style.display = "block";
const table = document.createElement("table");
table.className = "data-table";
const thead = document.createElement("thead");
const headerRow = document.createElement("tr");
headers.forEach((header) => {
const th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement("tbody");
data.forEach((obj) => {
const tr = document.createElement("tr");
headers.forEach((header) => {
const td = document.createElement("td");
const value = obj[header];
td.textContent = value !== null && value !== undefined ? value : "";
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
tableDisplay.innerHTML = "";
tableDisplay.appendChild(table);
}
// Find all arrays in the JSON structure
function findArrays(obj, path = "") {
const arrays = [];
if (Array.isArray(obj)) {
arrays.push({
path: path || "(root)",
data: obj,
count: obj.length,
});
} else if (obj && typeof obj === "object") {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const value = obj[key];
const newPath = path ? `${path}.${key}` : key;
if (Array.isArray(value)) {
arrays.push({
path: newPath,
data: value,
count: value.length,
});
} else if (value && typeof value === "object") {
arrays.push(...findArrays(value, newPath));
}
}
}
return arrays;
}
// Get data from selected path
function getDataFromPath(obj, path) {
if (path === "(root)") {
return obj;
}
const parts = path.split(".");
let current = obj;
for (const part of parts) {
if (current && typeof current === "object" && part in current) {
current = current[part];
} else {
return null;
}
}
return current;
}
// Populate array path selector
function populateArrayPaths(paths) {
arrayPath.innerHTML =
'<option value="">-- Select data array --</option>';
paths.forEach((pathInfo, index) => {
const option = document.createElement("option");
option.value = pathInfo.path;
option.textContent = `${pathInfo.path} (${pathInfo.count} item${
pathInfo.count !== 1 ? "s" : ""
})`;
arrayPath.appendChild(option);
});
// Auto-select if only one array found
if (paths.length === 1) {
arrayPath.value = paths[0].path;
}
}
// Convert and display data
function convertAndDisplay() {
if (!currentData) return;
const handling = nestedHandling.value;
const maxDepth =
handling === "flattenDepth" ? parseInt(depthLimit.value) : Infinity;
const processedData = processJSON(currentData, handling, maxDepth);
displayTable(processedData);
csvText = jsonToCSV(processedData);
csvOutput.textContent = csvText;
downloadBtn.disabled = false;
copyBtn.disabled = false;
// Expand panels if collapsed
csvHeader.classList.remove("collapsed");
csvContent.classList.remove("collapsed");
outputHeader.classList.remove("collapsed");
outputContent.classList.remove("collapsed");
}
// Parse JSON Lines format
function parseJSONLines(text) {
const lines = text.trim().split("\n");
const objects = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue; // Skip empty lines
try {
const obj = JSON.parse(line);
objects.push(obj);
} catch (error) {
// If parsing fails, it's not JSON Lines format
return null;
}
}
return objects.length > 0 ? objects : null;
}
// Load JSON file
fileInput.addEventListener("change", () => {
const file = fileInput.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const text = e.target.result;
let data;
// Try parsing as standard JSON first
try {
data = JSON.parse(text);
} catch (jsonError) {
// If standard JSON parsing fails, try JSON Lines format
data = parseJSONLines(text);
if (!data) {
throw new Error("Invalid JSON format. " + jsonError.message);
}
}
if (!data || (Array.isArray(data) && data.length === 0)) {
alert("JSON file appears to be empty");
return;
}
rawJsonData = data;
// Check if data is already an array at root level
if (Array.isArray(data)) {
currentData = data;
arrayPathGroup.style.display = "none";
convertAndDisplay();
} else {
// Find all arrays in the structure
detectedPaths = findArrays(data);
if (detectedPaths.length === 0) {
// No arrays found, treat the object itself as data
currentData = [data];
arrayPathGroup.style.display = "none";
convertAndDisplay();
} else if (detectedPaths.length === 1) {
// Only one array found, auto-select it
currentData = detectedPaths[0].data;
arrayPathGroup.style.display = "none";
convertAndDisplay();
} else {
// Multiple arrays found, let user choose
populateArrayPaths(detectedPaths);
arrayPathGroup.style.display = "block";
optionsHeader.classList.remove("collapsed");
optionsContent.classList.remove("collapsed");
// Don't convert yet, wait for user selection
tableDisplay.innerHTML =
'<span class="empty-state">Please select which data to convert from the dropdown above</span>';
csvOutput.innerHTML =
'<span class="empty-state">No data converted yet</span>';
downloadBtn.disabled = true;
copyBtn.disabled = true;
}
}
} catch (error) {
alert("Error parsing JSON file: " + error.message);
}
};
reader.onerror = () => {
alert("Error reading file");
};
reader.readAsText(file);
});
// Handle array path selection
arrayPath.addEventListener("change", () => {
const selectedPath = arrayPath.value;
if (!selectedPath || !rawJsonData) {
return;
}
const selectedData = getDataFromPath(rawJsonData, selectedPath);
if (selectedData && Array.isArray(selectedData)) {
currentData = selectedData;
convertAndDisplay();
} else {
alert("Could not retrieve data from selected path");
}
});
// Handle option changes
nestedHandling.addEventListener("change", () => {
// Show/hide depth limit option
if (nestedHandling.value === "flattenDepth") {
depthLimitGroup.style.display = "block";
} else {
depthLimitGroup.style.display = "none";
}
if (currentData) {
convertAndDisplay();
}
});
depthLimit.addEventListener("change", () => {
if (currentData && nestedHandling.value === "flattenDepth") {
convertAndDisplay();
}
});
// Copy to clipboard
copyBtn.addEventListener("click", () => {
const text = csvOutput.textContent;
if (!text) return;
navigator.clipboard
.writeText(text)
.then(() => {
const originalText = copyBtn.textContent;
copyBtn.textContent = "Copied!";
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
})
.catch((err) => {
alert("Could not copy: " + err);
});
});
// Download CSV
downloadBtn.addEventListener("click", () => {
if (!csvText) return;
const blob = new Blob([csvText], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "converted.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
const tableFullscreenBtn = document.getElementById("tableFullscreenBtn");
tableFullscreenBtn.addEventListener("click", () => {
if (!document.fullscreenElement) {
csvHeader.classList.remove("collapsed");
csvContent.classList.remove("collapsed");
document.getElementById("csvPanel").requestFullscreen();
} else {
document.exitFullscreen();
}
});
document.addEventListener("fullscreenchange", () => {
tableFullscreenBtn.textContent = document.fullscreenElement
? "Exit Fullscreen"
: "Fullscreen";
});
// Clear all data
clearBtn.addEventListener("click", () => {
fileInput.value = "";
currentData = null;
csvText = null;
rawJsonData = null;
detectedPaths = [];
arrayPathGroup.style.display = "none";
arrayPath.innerHTML =
'<option value="">-- Select data array --</option>';
tableDisplay.innerHTML =
'<span class="empty-state">No data loaded</span>';
csvOutput.innerHTML =
'<span class="empty-state">No data converted yet</span>';
columnCount.style.display = "none";
downloadBtn.disabled = true;
copyBtn.disabled = true;
});
</script>
</body>
</html>