Skip to content

Commit 30a947b

Browse files
authored
Create incomplete-json-printer.html
Created with GPT-4.5 and Canvas https://chatgpt.com/share/67dd9d55-7f70-8006-b55d-72730f60ddbe
1 parent 44c2299 commit 30a947b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

incomplete-json-printer.html

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Incomplete JSON Pretty Printer</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
margin: 20px;
11+
background-color: #f7f7f7;
12+
}
13+
textarea {
14+
width: 100%;
15+
height: 200px;
16+
font-family: monospace;
17+
font-size: 14px;
18+
}
19+
pre {
20+
background: #fff;
21+
padding: 10px;
22+
border: 1px solid #ddd;
23+
border-radius: 4px;
24+
white-space: pre-wrap;
25+
word-wrap: break-word;
26+
}
27+
</style>
28+
</head>
29+
<body>
30+
<h2>Incomplete JSON Pretty Printer</h2>
31+
<p>Pretty-print JSON even if it is truncated and not a full document.</p>
32+
<textarea id="inputJson" placeholder="Paste incomplete JSON here..."></textarea>
33+
<br>
34+
<button onclick="prettyPrintJson()">Pretty Print JSON</button>
35+
<h3>Formatted JSON:</h3>
36+
<pre id="outputJson"></pre>
37+
38+
<script>
39+
function prettyPrintJson() {
40+
const input = document.getElementById('inputJson').value;
41+
const formattedJson = prettyFormat(input);
42+
document.getElementById('outputJson').textContent = formattedJson;
43+
}
44+
45+
function prettyFormat(jsonString) {
46+
let result = '';
47+
let indent = 0;
48+
let inString = false;
49+
const indentStep = 2;
50+
51+
for (let i = 0; i < jsonString.length; i++) {
52+
const char = jsonString[i];
53+
const prevChar = i > 0 ? jsonString[i - 1] : '';
54+
55+
if (char === '"' && prevChar !== '\\') {
56+
inString = !inString;
57+
result += char;
58+
} else if (!inString && (char === '{' || char === '[')) {
59+
indent += indentStep;
60+
result += char + '\n' + ' '.repeat(indent);
61+
} else if (!inString && (char === '}' || char === ']')) {
62+
indent -= indentStep;
63+
result += '\n' + ' '.repeat(indent) + char;
64+
} else if (!inString && char === ',') {
65+
result += char + '\n' + ' '.repeat(indent);
66+
} else {
67+
result += char;
68+
}
69+
}
70+
71+
return result;
72+
}
73+
</script>
74+
</body>
75+
</html>

0 commit comments

Comments
 (0)