-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-formatter.html
More file actions
537 lines (504 loc) · 18.4 KB
/
json-formatter.html
File metadata and controls
537 lines (504 loc) · 18.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Formatter - Format, Minify, Validate, and Sort Keys</title>
<meta name="description" content="Free JSON formatter and validator. Format, minify, sort keys, and inspect parse errors with line/column details. Runs entirely in your browser.">
<meta property="og:title" content="JSON Formatter">
<meta property="og:description" content="Format, minify, validate, and sort JSON in your browser.">
<meta property="og:type" content="website">
<meta name="twitter:card" content="summary">
<meta name="theme-color" content="#111827">
<style>
:root {
--bg: #0a0f1f;
--bg-soft: #111b35;
--panel: #0f172d;
--panel-2: #12203f;
--text: #eef3ff;
--muted: #a4b2d8;
--line: rgba(255, 255, 255, 0.12);
--accent: #29d3b5;
--accent-2: #ffd166;
--danger: #ff6b6b;
--ok: #52d273;
--shadow: 0 20px 48px rgba(0, 0, 0, 0.35);
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
color: var(--text);
font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
background:
radial-gradient(circle at 10% 0%, rgba(41, 211, 181, 0.2), transparent 30%),
radial-gradient(circle at 100% 10%, rgba(255, 209, 102, 0.16), transparent 30%),
linear-gradient(145deg, var(--bg), var(--bg-soft));
}
main {
max-width: 1150px;
margin: 0 auto;
padding: 28px 18px 42px;
}
.hero {
border: 1px solid var(--line);
border-radius: 16px;
padding: 22px;
background: linear-gradient(145deg, rgba(18, 32, 63, 0.86), rgba(9, 15, 32, 0.9));
box-shadow: var(--shadow);
}
.hero h1 {
margin: 0 0 8px;
font-size: clamp(1.7rem, 3.8vw, 2.4rem);
letter-spacing: -0.02em;
}
.hero p {
margin: 0;
max-width: 78ch;
color: var(--muted);
}
.toolbar {
margin-top: 16px;
display: grid;
grid-template-columns: 1fr;
gap: 10px;
}
.toolbar-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
}
.btn {
border: 1px solid var(--line);
background: var(--panel);
color: var(--text);
border-radius: 10px;
padding: 10px 14px;
cursor: pointer;
font-weight: 600;
transition: transform 120ms ease, border-color 120ms ease, background-color 120ms ease;
}
.btn:hover {
transform: translateY(-1px);
border-color: rgba(41, 211, 181, 0.55);
background: var(--panel-2);
}
.btn.primary {
background: linear-gradient(145deg, var(--accent), #1bb99e);
color: #061525;
border-color: transparent;
}
.btn.secondary {
background: linear-gradient(145deg, var(--accent-2), #ffc342);
color: #2f1f00;
border-color: transparent;
}
.inline {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 0.92rem;
}
select {
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel);
color: var(--text);
padding: 7px 9px;
}
.grid {
margin-top: 14px;
display: grid;
gap: 12px;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.pane {
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(13, 22, 42, 0.86);
overflow: hidden;
}
.pane-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
padding: 10px 12px;
border-bottom: 1px solid var(--line);
font-size: 0.95rem;
}
.pane-head strong {
font-weight: 700;
letter-spacing: 0.01em;
}
textarea {
width: 100%;
min-height: 370px;
border: 0;
margin: 0;
resize: vertical;
padding: 14px;
background: transparent;
color: var(--text);
font: 14px/1.55 "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
}
textarea:focus {
outline: 2px solid rgba(41, 211, 181, 0.5);
outline-offset: -2px;
}
.status {
margin-top: 12px;
border: 1px solid var(--line);
border-radius: 12px;
padding: 10px 12px;
background: rgba(10, 17, 34, 0.82);
color: var(--muted);
font-size: 0.92rem;
}
.status.ok {
border-color: rgba(82, 210, 115, 0.45);
color: #b8f0c7;
}
.status.error {
border-color: rgba(255, 107, 107, 0.45);
color: #ffd1d1;
}
.stats {
margin-top: 8px;
color: var(--muted);
font-size: 0.85rem;
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.nav {
margin-top: 16px;
display: flex;
flex-wrap: wrap;
gap: 12px;
font-size: 0.92rem;
}
.nav a {
color: #9bd9ff;
text-decoration: none;
}
.nav a:hover {
text-decoration: underline;
}
@media (max-width: 720px) {
textarea { min-height: 280px; }
.hero { padding: 18px; }
}
</style>
</head>
<body>
<main>
<section class="hero">
<h1>JSON Formatter</h1>
<p>Format, minify, validate, and sort JSON quickly. No uploads, no server calls, and no tracking scripts.</p>
<div class="toolbar">
<div class="toolbar-row">
<button class="btn primary" id="formatBtn">Format</button>
<button class="btn" id="minifyBtn">Minify</button>
<button class="btn" id="sortBtn">Sort Keys</button>
<button class="btn secondary" id="sampleBtn">Load Sample</button>
<button class="btn" id="clearBtn">Clear</button>
</div>
<div class="toolbar-row">
<label class="inline" for="indentSize">Indent</label>
<select id="indentSize">
<option value="2">2 spaces</option>
<option value="4">4 spaces</option>
<option value="tab">Tab</option>
</select>
<button class="btn" id="copyBtn">Copy Output</button>
<button class="btn" id="downloadBtn">Download</button>
<span class="inline" id="sizeLabel">Input: 0 chars</span>
</div>
</div>
</section>
<section class="grid">
<article class="pane">
<div class="pane-head">
<strong>Input JSON</strong>
<span>paste or type</span>
</div>
<textarea id="input" spellcheck="false" placeholder='{"message":"hello","items":[1,2,3]}'></textarea>
</article>
<article class="pane">
<div class="pane-head">
<strong>Output</strong>
<span id="outputMode">formatted</span>
</div>
<textarea id="output" spellcheck="false" readonly placeholder="Formatted output appears here"></textarea>
</article>
</section>
<div class="status" id="status">Ready.</div>
<div class="stats" id="stats">
<span>Lines: 0</span>
<span>Keys: 0</span>
<span>Size: 0 bytes</span>
</div>
<nav class="nav" aria-label="Related links">
<a href="/index.html">Home</a>
<a href="/python-formatter.html">Python Formatter</a>
<a href="/regex-cheatsheet.html">Regex Cheat Sheet</a>
<a href="/typescript-utility-types-guide.html">TypeScript Utility Types</a>
</nav>
</main>
<script>
const inputEl = document.getElementById("input");
const outputEl = document.getElementById("output");
const statusEl = document.getElementById("status");
const statsEl = document.getElementById("stats");
const modeEl = document.getElementById("outputMode");
const sizeLabelEl = document.getElementById("sizeLabel");
const indentEl = document.getElementById("indentSize");
function getIndent() {
return indentEl.value === "tab" ? "\t" : Number.parseInt(indentEl.value, 10);
}
function setStatus(message, kind) {
statusEl.textContent = message;
statusEl.className = "status";
if (kind) {
statusEl.classList.add(kind);
}
}
function formatNumber(n) {
return new Intl.NumberFormat("en-US").format(n);
}
function countKeys(value) {
if (Array.isArray(value)) {
return value.reduce((sum, item) => sum + countKeys(item), 0);
}
if (value && typeof value === "object") {
return Object.keys(value).length + Object.values(value).reduce((sum, item) => sum + countKeys(item), 0);
}
return 0;
}
function sortKeysDeep(value) {
if (Array.isArray(value)) {
return value.map(sortKeysDeep);
}
if (value && typeof value === "object") {
const out = {};
Object.keys(value)
.sort((a, b) => a.localeCompare(b))
.forEach((key) => {
out[key] = sortKeysDeep(value[key]);
});
return out;
}
return value;
}
function stripCommentsAndTrailingCommas(source) {
let out = "";
let inString = false;
let escaping = false;
let inLineComment = false;
let inBlockComment = false;
for (let i = 0; i < source.length; i += 1) {
const ch = source[i];
const next = source[i + 1] || "";
if (inLineComment) {
if (ch === "\n") {
inLineComment = false;
out += ch;
}
continue;
}
if (inBlockComment) {
if (ch === "*" && next === "/") {
inBlockComment = false;
i += 1;
}
continue;
}
if (!inString && ch === "/" && next === "/") {
inLineComment = true;
i += 1;
continue;
}
if (!inString && ch === "/" && next === "*") {
inBlockComment = true;
i += 1;
continue;
}
out += ch;
if (inString) {
if (escaping) {
escaping = false;
} else if (ch === "\\") {
escaping = true;
} else if (ch === '"') {
inString = false;
}
} else if (ch === '"') {
inString = true;
}
}
let cleaned = "";
inString = false;
escaping = false;
for (let i = 0; i < out.length; i += 1) {
const ch = out[i];
if (inString) {
cleaned += ch;
if (escaping) {
escaping = false;
} else if (ch === "\\") {
escaping = true;
} else if (ch === '"') {
inString = false;
}
continue;
}
if (ch === '"') {
inString = true;
cleaned += ch;
continue;
}
if (ch === ",") {
let j = i + 1;
while (j < out.length && /\s/.test(out[j])) {
j += 1;
}
if (out[j] === "}" || out[j] === "]") {
continue;
}
}
cleaned += ch;
}
return cleaned;
}
function parseInput(source) {
const trimmed = source.trim();
if (!trimmed) {
throw new Error("Input is empty.");
}
try {
return JSON.parse(trimmed);
} catch (strictErr) {
const lenient = stripCommentsAndTrailingCommas(trimmed);
try {
return JSON.parse(lenient);
} catch (_lenientErr) {
throw strictErr;
}
}
}
function updateStats(parsed, outputText) {
const lineCount = outputText ? outputText.split(/\r?\n/).length : 0;
const keyCount = parsed == null ? 0 : countKeys(parsed);
const byteCount = new TextEncoder().encode(outputText || "").length;
statsEl.innerHTML = [
`<span>Lines: ${formatNumber(lineCount)}</span>`,
`<span>Keys: ${formatNumber(keyCount)}</span>`,
`<span>Size: ${formatNumber(byteCount)} bytes</span>`
].join("");
}
function writeOutput(text, parsed, mode) {
outputEl.value = text;
modeEl.textContent = mode;
updateStats(parsed, text);
setStatus("JSON parsed successfully.", "ok");
}
function run(action) {
const raw = inputEl.value;
sizeLabelEl.textContent = `Input: ${formatNumber(raw.length)} chars`;
try {
let parsed = parseInput(raw);
let text;
if (action === "sort") {
parsed = sortKeysDeep(parsed);
text = JSON.stringify(parsed, null, getIndent());
writeOutput(text, parsed, "sorted");
return;
}
if (action === "minify") {
text = JSON.stringify(parsed);
writeOutput(text, parsed, "minified");
return;
}
text = JSON.stringify(parsed, null, getIndent());
writeOutput(text, parsed, "formatted");
} catch (err) {
outputEl.value = "";
updateStats(null, "");
const message = err && err.message ? err.message : "Invalid JSON.";
setStatus(`Validation error: ${message}`, "error");
}
}
document.getElementById("formatBtn").addEventListener("click", () => run("format"));
document.getElementById("minifyBtn").addEventListener("click", () => run("minify"));
document.getElementById("sortBtn").addEventListener("click", () => run("sort"));
document.getElementById("sampleBtn").addEventListener("click", () => {
inputEl.value = JSON.stringify({
service: "payments-api",
env: "prod",
limits: {
requestsPerMinute: 240,
burst: 32
},
features: ["idempotency", "webhooks", "audit-log"],
retryPolicy: {
maxRetries: 3,
backoffMs: [200, 500, 1000]
},
enabled: true
}, null, 2);
sizeLabelEl.textContent = `Input: ${formatNumber(inputEl.value.length)} chars`;
run("format");
});
document.getElementById("clearBtn").addEventListener("click", () => {
inputEl.value = "";
outputEl.value = "";
modeEl.textContent = "formatted";
sizeLabelEl.textContent = "Input: 0 chars";
updateStats(null, "");
setStatus("Cleared.");
});
document.getElementById("copyBtn").addEventListener("click", async () => {
if (!outputEl.value) {
setStatus("Nothing to copy yet.", "error");
return;
}
try {
await navigator.clipboard.writeText(outputEl.value);
setStatus("Output copied to clipboard.", "ok");
} catch (_err) {
outputEl.focus();
outputEl.select();
setStatus("Clipboard access blocked. Selected output instead.");
}
});
document.getElementById("downloadBtn").addEventListener("click", () => {
if (!outputEl.value) {
setStatus("Nothing to download yet.", "error");
return;
}
const blob = new Blob([outputEl.value], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "formatted.json";
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
setStatus("Downloaded formatted.json", "ok");
});
inputEl.addEventListener("input", () => {
sizeLabelEl.textContent = `Input: ${formatNumber(inputEl.value.length)} chars`;
});
indentEl.addEventListener("change", () => {
if (outputEl.value) {
run(modeEl.textContent === "minified" ? "minify" : modeEl.textContent === "sorted" ? "sort" : "format");
}
});
</script>
</body>
</html>