-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
255 lines (215 loc) · 6.88 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>JSON Viewer</title>
<style>
/* CSS styling for the JSON viewer */
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
h1 {
text-align: center;
color: #333;
margin-top: 0;
}
label {
font-weight: bold;
color: #555;
display: block;
margin-bottom: 10px;
}
#json-input {
width: 100%;
height: 200px;
border: 1px solid #ccc;
padding: 10px;
font-family: Consolas, monospace;
}
#json-output {
width: 100%;
border: 1px solid #ccc;
padding: 10px;
background-color: #f9f9f9;
font-family: Consolas, monospace;
white-space: pre-wrap;
overflow-x: auto;
}
.error-message {
color: red;
font-weight: bold;
margin-top: 10px;
}
.toggle-btn {
display: block;
margin-top: 20px;
cursor: pointer;
color: #555;
text-decoration: underline;
transition: color 0.3s;
}
.toggle-btn:hover {
color: #333;
}
.hidden {
display: none;
}
.json-properties {
margin-left: 20px;
}
.json-property {
margin-bottom: 10px;
}
.json-key {
color: #2d96b8;
font-weight: bold;
}
.json-string {
color: #7bc18e;
}
.json-boolean {
color: #f48c06;
}
.json-number {
color: #9a5cb3;
}
.export-btn {
display: block;
margin-top: 20px;
background-color: #2d96b8;
color: #fff;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.export-btn:hover {
background-color: #1b6e82;
}
</style>
</head>
<body>
<div class="container">
<h1>JSON Viewer</h1>
<label for="json-input">Enter JSON:</label>
<textarea id="json-input" placeholder="Enter valid JSON here..."></textarea>
<h2>Parsed JSON:</h2>
<pre id="json-output"></pre>
<p class="error-message" id="error-message"></p>
<div id="toggle-btn" class="toggle-btn hidden">Toggle Properties</div>
<div id="json-properties" class="json-properties hidden"></div>
<button id="export-btn" class="export-btn hidden">Export Parsed JSON</button>
</div>
<script>
// Get references to HTML elements
const jsonInput = document.getElementById('json-input');
const jsonOutput = document.getElementById('json-output');
const errorMessage = document.getElementById('error-message');
const toggleBtn = document.getElementById('toggle-btn');
const jsonProperties = document.getElementById('json-properties');
const exportBtn = document.getElementById('export-btn');
// Add event listener for input changes
jsonInput.addEventListener('input', function() {
const inputText = jsonInput.value;
try {
// Parse the JSON input
const parsedJSON = JSON.parse(inputText);
// Convert the parsed JSON back to a string with proper indentation
const jsonString = JSON.stringify(parsedJSON, null, 2);
// Set the formatted JSON string as the output
jsonOutput.innerHTML = syntaxHighlight(jsonString);
errorMessage.textContent = ''; // Clear any previous error messages
// Generate JSON properties
generateProperties(parsedJSON);
// Show the toggle button and properties
toggleBtn.classList.remove('hidden');
jsonProperties.classList.remove('hidden');
exportBtn.classList.remove('hidden');
} catch (error) {
// If there's an error parsing the JSON, display the error message
jsonOutput.textContent = '';
errorMessage.textContent = 'Invalid JSON: ' + error.message;
// Hide the toggle button, properties, and export button
toggleBtn.classList.add('hidden');
jsonProperties.classList.add('hidden');
exportBtn.classList.add('hidden');
}
});
// Toggle properties visibility
toggleBtn.addEventListener('click', function() {
jsonProperties.classList.toggle('hidden');
});
// Export parsed JSON to local storage
exportBtn.addEventListener('click', function() {
const parsedJSON = JSON.parse(jsonInput.value);
localStorage.setItem('parsedJSON', JSON.stringify(parsedJSON));
alert('Parsed JSON exported to local storage!');
});
// Generate JSON properties
function generateProperties(obj, parent = jsonProperties) {
parent.innerHTML = '';
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
const propertyContainer = document.createElement('div');
propertyContainer.classList.add('json-property');
const keyElement = document.createElement('span');
keyElement.classList.add('json-key');
keyElement.textContent = key + ': ';
const valueElement = document.createElement('span');
if (typeof value === 'object') {
valueElement.textContent = '{ ... }';
valueElement.classList.add('json-object');
generateProperties(value, propertyContainer);
} else {
valueElement.textContent = getValueString(value);
}
propertyContainer.appendChild(keyElement);
propertyContainer.appendChild(valueElement);
parent.appendChild(propertyContainer);
}
}
}
// Get string representation of different value types
function getValueString(value) {
if (typeof value === 'string') {
return '"' + value + '"';
} else if (typeof value === 'number') {
return value.toString();
} else if (typeof value === 'boolean') {
return value.toString();
} else {
return '';
}
}
// Syntax highlighting for JSON output
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g, function (match) {
let cls = 'json-number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key';
} else {
cls = 'json-string';
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
</script>
</body>
</html>