-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull.html
More file actions
183 lines (165 loc) · 7.21 KB
/
full.html
File metadata and controls
183 lines (165 loc) · 7.21 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Analyzer - All Keys</title>
<style>
*{box-sizing:border-box;margin:0}
body{font:16px/1.5 sans-serif;color:#333;background:#f4f4f4;padding:20px}
.container{max-width:1200px;margin:auto;background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 4px rgba(0,0,0,.1)}
h1{text-align:center;color:#2c3e50;margin-bottom:20px}
.card{background:#fff;border:1px solid #e0e0e0;border-radius:8px;padding:20px;margin-bottom:20px}
.file-upload{display:flex;align-items:center;margin-bottom:15px}
.file-upload input[type="file"]{display:none}
.file-upload label{background:#3498db;color:#fff;padding:10px 15px;border-radius:5px;cursor:pointer}
.file-upload label:hover{background:#2980b9}
#fileCount{margin-left:15px;color:#7f8c8d}
.button-group{display:flex;gap:10px;margin-bottom:20px}
button{background:#2ecc71;color:#fff;border:none;padding:10px 20px;cursor:pointer;border-radius:5px}
button:hover{background:#27ae60}
button:disabled{background:#95a5a6;cursor:not-allowed}
.table-container{width:100%;overflow-x:auto;border-radius:8px;box-shadow:0 2px 4px rgba(0,0,0,.1)}
table{width:100%;border-collapse:separate;border-spacing:0;background:#fff}
th,td{padding:10px;text-align:left;border-bottom:1px solid #e0e0e0}
th{background:#f2f2f2;font-weight:bold;color:#2c3e50;position:sticky;top:0;z-index:10}
.key-column{position:sticky;left:0;background:#f2f2f2;z-index:20}
.changed{background:#fff9c4}
.cell-content{max-height:100px;overflow-y:auto;word-break:break-word}
.alert{background:#f8d7da;color:#721c24;padding:15px;border-radius:5px;margin-bottom:20px}
.hidden{display:none}
@media (max-width:768px){.container{padding:10px}.button-group{flex-direction:column}button{width:100%}}
</style>
</head>
<body>
<div class="container">
<h1>JSON Analyzer - All Keys</h1>
<div class="card">
<div class="file-upload">
<label for="fileUpload">Choose Files</label>
<input type="file" id="fileUpload" multiple accept=".json">
<span id="fileCount">0 file(s) selected</span>
</div>
</div>
<div class="button-group">
<button id="analyzeButton">Analyze</button>
<button id="exportJsonButton" class="hidden">Export JSON</button>
<button id="exportCsvButton" class="hidden">Export CSV</button>
<button id="copyTableButton" class="hidden">Copy Table</button>
</div>
<div id="errorAlert" class="alert hidden"></div>
<div id="resultCard" class="card hidden">
<div class="table-container" id="analysisTable"></div>
</div>
</div>
<script>
let files = [], payloads = [];
const $ = document.querySelector.bind(document);
const $$ = (sel, el = document) => Array.from(el.querySelectorAll(sel));
$('#fileUpload').addEventListener('change', e => {
files = Array.from(e.target.files);
$('#fileCount').textContent = `${files.length} file(s) selected`;
$('#analyzeButton').disabled = !files.length;
});
$('#analyzeButton').addEventListener('click', analyzeJson);
$('#exportJsonButton').addEventListener('click', () => exportData('json'));
$('#exportCsvButton').addEventListener('click', () => exportData('csv'));
$('#copyTableButton').addEventListener('click', copyTable);
async function analyzeJson() {
try {
payloads = await Promise.all(files.map(readFileAsJson));
renderTable();
$$('.hidden').forEach(el => el.classList.remove('hidden'));
} catch (error) {
showError(error.message);
}
}
function readFileAsJson(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
try {
resolve({ fileName: file.name, content: JSON.parse(e.target.result) });
} catch (error) {
reject(new Error(`Failed to parse ${file.name}: ${error.message}`));
}
};
reader.onerror = () => reject(new Error(`Failed to read ${file.name}`));
reader.readAsText(file);
});
}
function renderTable() {
const allKeys = new Set();
payloads.forEach(payload => collectKeys(payload.content, '', allKeys));
let html = '<table><thead><tr><th class="key-column">Key Base</th><th class="key-column">Key Attribute</th>';
payloads.forEach(payload => html += `<th>${payload.fileName}</th>`);
html += '</tr></thead><tbody>';
allKeys.forEach(key => {
const [keyBase, keyAttribute] = splitKey(key);
html += `<tr><th class="key-column">${keyBase}</th><th class="key-column">${keyAttribute}</th>`;
payloads.forEach((payload, i) => {
const value = getNestedValue(payload.content, key);
const prevValue = i > 0 ? getNestedValue(payloads[i-1].content, key) : undefined;
const hasChanged = JSON.stringify(value) !== JSON.stringify(prevValue);
html += `<td class="${hasChanged ? 'changed' : ''}"><div class="cell-content">${formatValue(value)}</div></td>`;
});
html += '</tr>';
});
$('#analysisTable').innerHTML = html + '</tbody></table>';
$('#resultCard').classList.remove('hidden');
}
function splitKey(key) {
const parts = key.split('.');
if (parts.length === 1) return [parts[0], ''];
return [parts.slice(0, -1).join('.'), parts[parts.length - 1]];
}
function collectKeys(obj, prefix, keySet) {
Object.keys(obj).forEach(key => {
const fullKey = prefix ? `${prefix}.${key}` : key;
keySet.add(fullKey);
if (typeof obj[key] === 'object' && obj[key] !== null) {
collectKeys(obj[key], fullKey, keySet);
}
});
}
function getNestedValue(obj, key) {
return key.split('.').reduce((o, k) => (o || {})[k], obj);
}
function formatValue(value) {
if (value === undefined) return '';
return typeof value === 'object' ? JSON.stringify(value) : String(value);
}
function exportData(type) {
const allKeys = new Set();
payloads.forEach(payload => collectKeys(payload.content, '', allKeys));
const content = type === 'json' ? JSON.stringify(payloads, null, 2) :
[['Key Base', 'Key Attribute', ...payloads.map(p => p.fileName)].join(',')]
.concat(Array.from(allKeys)
.map(key => {
const [keyBase, keyAttribute] = splitKey(key);
return [keyBase, keyAttribute, ...payloads.map(p => JSON.stringify(getNestedValue(p.content, key) ?? ''))].join(',');
})).join('\n');
const blob = new Blob([content], { type: type === 'json' ? 'application/json' : 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `analysis.${type}`;
a.click();
}
function copyTable() {
const tableHtml = $('#analysisTable').innerHTML;
const blob = new Blob([tableHtml], { type: 'text/html' });
const clipboardItem = new ClipboardItem({ 'text/html': blob });
navigator.clipboard.write([clipboardItem]).then(() => {
alert('Table copied to clipboard!');
}).catch(err => {
console.error('Failed to copy table: ', err);
alert('Failed to copy table. Please try again.');
});
}
function showError(message) {
$('#errorAlert').textContent = message;
$('#errorAlert').classList.remove('hidden');
}
</script>
</body>
</html>