-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.html
More file actions
411 lines (356 loc) · 13.9 KB
/
json.html
File metadata and controls
411 lines (356 loc) · 13.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Formatter — Friday</title>
<meta name="description" content="Paste JSON, get it formatted with syntax highlighting. Click any key to copy its path. Validates on paste.">
<style>
:root {
--bg: #0a0a0f;
--surface: #12121a;
--border: #1e1e2e;
--text: #c8c8d4;
--text-dim: #6e6e82;
--accent: #7c9cff;
--accent-dim: #4a6abf;
--green: #7ccc7c;
--orange: #ccaa7c;
--red: #cc7c7c;
--purple: #b07ccc;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: var(--bg);
color: var(--text);
font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', 'Cascadia Code', monospace;
font-size: 14px;
line-height: 1.6;
min-height: 100vh;
}
.container {
max-width: 960px;
margin: 0 auto;
padding: 3rem 2rem;
}
.back {
font-size: 0.85rem;
color: var(--text-dim);
margin-bottom: 1.5rem;
}
.back a { color: var(--accent); text-decoration: none; }
.back a:hover { text-decoration: underline; }
h1 {
font-size: 1.4rem;
font-weight: 600;
color: var(--accent);
margin-bottom: 0.25rem;
}
.subtitle {
color: var(--text-dim);
font-size: 0.85rem;
margin-bottom: 1.5rem;
}
.toolbar {
display: flex;
gap: 0.5rem;
margin-bottom: 0.75rem;
flex-wrap: wrap;
align-items: center;
}
.btn {
padding: 0.4rem 0.8rem;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 4px;
color: var(--text);
font-family: inherit;
font-size: 0.8rem;
cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.active { border-color: var(--accent); color: var(--accent); }
.stats {
margin-left: auto;
font-size: 0.75rem;
color: var(--text-dim);
}
.error-bar {
background: #2a1515;
border: 1px solid #4a2020;
border-radius: 4px;
padding: 0.5rem 0.8rem;
margin-bottom: 0.75rem;
font-size: 0.85rem;
color: var(--red);
display: none;
}
.path-bar {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 4px;
padding: 0.4rem 0.8rem;
margin-bottom: 0.75rem;
font-size: 0.8rem;
color: var(--accent);
display: none;
cursor: pointer;
}
.path-bar:hover { border-color: var(--accent); }
.editor {
display: flex;
gap: 1rem;
min-height: 400px;
}
textarea {
flex: 1;
min-height: 400px;
padding: 1rem;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
color: var(--text);
font-family: inherit;
font-size: 0.85rem;
line-height: 1.5;
resize: vertical;
tab-size: 2;
}
textarea:focus { outline: none; border-color: var(--accent-dim); }
.output {
flex: 1;
min-height: 400px;
padding: 1rem;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
overflow: auto;
font-size: 0.85rem;
line-height: 1.5;
white-space: pre;
}
.output .key { color: var(--accent); cursor: pointer; }
.output .key:hover { text-decoration: underline; }
.output .str { color: var(--green); }
.output .num { color: var(--orange); }
.output .bool { color: var(--purple); }
.output .null { color: var(--text-dim); }
.output .bracket { color: var(--text-dim); }
.output .comma { color: var(--text-dim); }
.copied-toast {
position: fixed;
bottom: 2rem;
left: 50%;
transform: translateX(-50%);
background: var(--accent);
color: var(--bg);
padding: 0.5rem 1.2rem;
border-radius: 4px;
font-size: 0.85rem;
font-weight: 600;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none;
}
.copied-toast.show { opacity: 1; }
footer {
margin-top: 3rem;
padding-top: 1.5rem;
border-top: 1px solid var(--border);
font-size: 0.75rem;
color: var(--text-dim);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
@media (max-width: 700px) {
.container { padding: 2rem 1rem; }
.editor { flex-direction: column; }
textarea, .output { min-height: 250px; }
}
</style>
</head>
<body>
<div class="container">
<div class="back"><a href="/">friday</a> / <a href="/tools/cron">tools</a></div>
<h1>JSON Formatter</h1>
<div class="subtitle">Paste JSON. Get it formatted with syntax highlighting. Click any key to copy its path.</div>
<div class="toolbar">
<button class="btn" onclick="formatJSON(2)">2 spaces</button>
<button class="btn active" onclick="formatJSON(4)">4 spaces</button>
<button class="btn" onclick="formatJSON('tab')">Tabs</button>
<button class="btn" onclick="minifyJSON()">Minify</button>
<button class="btn" onclick="copyOutput()">Copy output</button>
<button class="btn" onclick="clearAll()">Clear</button>
<div class="stats" id="stats"></div>
</div>
<div class="error-bar" id="error-bar"></div>
<div class="path-bar" id="path-bar" onclick="copyPath()" title="Click to copy path"></div>
<div class="editor">
<textarea id="input" placeholder="Paste your JSON here..." spellcheck="false"></textarea>
<div class="output" id="output"></div>
</div>
<div class="copied-toast" id="toast">Copied!</div>
<footer>
<p><a href="/">Home</a> · <a href="/tools/cron">Cron Explainer</a> · <a href="/tools/timestamp">Timestamp</a> · <a href="/tools/regex">Regex Tester</a> · <a href="/tools/base64">Base64</a> · <a href="/writing">Writing</a></p>
</footer>
</div>
<script>
const input = document.getElementById('input');
const output = document.getElementById('output');
const errorBar = document.getElementById('error-bar');
const pathBar = document.getElementById('path-bar');
const statsEl = document.getElementById('stats');
let currentIndent = 4;
let parsedData = null;
input.addEventListener('input', () => formatJSON(currentIndent));
function showToast(msg) {
const toast = document.getElementById('toast');
toast.textContent = msg || 'Copied!';
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 1000);
}
function clearAll() {
input.value = '';
output.innerHTML = '';
errorBar.style.display = 'none';
pathBar.style.display = 'none';
statsEl.textContent = '';
parsedData = null;
}
function countNodes(obj) {
if (obj === null || typeof obj !== 'object') return 1;
let count = 1;
if (Array.isArray(obj)) {
for (const item of obj) count += countNodes(item);
} else {
for (const key of Object.keys(obj)) count += countNodes(obj[key]);
}
return count;
}
function formatJSON(indent) {
const raw = input.value.trim();
errorBar.style.display = 'none';
pathBar.style.display = 'none';
if (!raw) {
output.innerHTML = '';
statsEl.textContent = '';
parsedData = null;
return;
}
try {
parsedData = JSON.parse(raw);
} catch (e) {
errorBar.textContent = e.message;
errorBar.style.display = 'block';
output.innerHTML = '';
statsEl.textContent = '';
parsedData = null;
return;
}
if (indent === 'tab') {
currentIndent = 'tab';
} else {
currentIndent = indent;
}
// Update active button
document.querySelectorAll('.toolbar .btn').forEach(b => b.classList.remove('active'));
const labels = {'2': '2 spaces', '4': '4 spaces', 'tab': 'Tabs'};
document.querySelectorAll('.toolbar .btn').forEach(b => {
if (b.textContent === labels[String(indent)]) b.classList.add('active');
});
const nodes = countNodes(parsedData);
const depth = maxDepth(parsedData);
statsEl.textContent = `${nodes} values · ${depth} levels deep · ${raw.length.toLocaleString()} chars`;
output.innerHTML = renderValue(parsedData, '', 0);
}
function maxDepth(obj) {
if (obj === null || typeof obj !== 'object') return 0;
let max = 0;
const children = Array.isArray(obj) ? obj : Object.values(obj);
for (const child of children) {
max = Math.max(max, maxDepth(child));
}
return max + 1;
}
function minifyJSON() {
if (!parsedData) return;
const minified = JSON.stringify(parsedData);
input.value = minified;
output.textContent = minified;
statsEl.textContent = `${minified.length.toLocaleString()} chars (minified)`;
}
function copyOutput() {
if (!parsedData) return;
const indentStr = currentIndent === 'tab' ? '\t' : ' '.repeat(currentIndent);
const text = JSON.stringify(parsedData, null, indentStr);
navigator.clipboard.writeText(text).then(() => showToast('Copied formatted JSON'));
}
function copyPath() {
const path = pathBar.textContent;
navigator.clipboard.writeText(path).then(() => showToast('Copied: ' + path));
}
function getIndent(level) {
if (currentIndent === 'tab') return '\t'.repeat(level);
return ' '.repeat(currentIndent * level);
}
function escapeHtml(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function renderValue(val, path, level) {
if (val === null) return '<span class="null">null</span>';
if (typeof val === 'boolean') return `<span class="bool">${val}</span>`;
if (typeof val === 'number') return `<span class="num">${val}</span>`;
if (typeof val === 'string') return `<span class="str">"${escapeHtml(val)}"</span>`;
if (Array.isArray(val)) {
if (val.length === 0) return '<span class="bracket">[]</span>';
let html = '<span class="bracket">[</span>\n';
val.forEach((item, i) => {
const itemPath = `${path}[${i}]`;
html += getIndent(level + 1);
html += renderValue(item, itemPath, level + 1);
if (i < val.length - 1) html += '<span class="comma">,</span>';
html += '\n';
});
html += getIndent(level) + '<span class="bracket">]</span>';
return html;
}
if (typeof val === 'object') {
const keys = Object.keys(val);
if (keys.length === 0) return '<span class="bracket">{}</span>';
let html = '<span class="bracket">{</span>\n';
keys.forEach((key, i) => {
const keyPath = path ? `${path}.${key}` : key;
html += getIndent(level + 1);
html += `<span class="key" data-path="${escapeHtml(keyPath)}" onclick="showPath(this)">"${escapeHtml(key)}"</span>: `;
html += renderValue(val[key], keyPath, level + 1);
if (i < keys.length - 1) html += '<span class="comma">,</span>';
html += '\n';
});
html += getIndent(level) + '<span class="bracket">}</span>';
return html;
}
return escapeHtml(String(val));
}
function showPath(el) {
const path = el.getAttribute('data-path');
pathBar.textContent = path;
pathBar.style.display = 'block';
}
// Handle tab key in textarea
input.addEventListener('keydown', function(e) {
if (e.key === 'Tab') {
e.preventDefault();
const start = this.selectionStart;
const end = this.selectionEnd;
this.value = this.value.substring(0, start) + ' ' + this.value.substring(end);
this.selectionStart = this.selectionEnd = start + 2;
}
});
// Auto-format on paste
input.addEventListener('paste', function() {
setTimeout(() => formatJSON(currentIndent), 10);
});
</script>
<script src="/nav.js"></script>
</body>
</html>