-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonviz.html
110 lines (98 loc) · 3.39 KB
/
jsonviz.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
<!DOCTYPE html>
<html>
<head>
<title>JSON Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #333;
color: #fff;
}
#jsonViewer {
border: 1px solid #555;
padding: 10px;
background-color: #444;
max-width: 800px;
margin: 0 auto;
}
#jsonInput {
width: 100%;
max-width: 100%;
box-sizing: border-box;
resize: vertical;
}
button {
background-color: #007bff;
color: #fff;
border: none;
padding: 5px 10px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#visualization {
margin-top: 10px;
}
ul {
list-style-type: none;
padding-left: 20px;
}
</style>
</head>
<body>
<div id="jsonViewer">
<h2>JSON Viewer</h2>
<textarea id="jsonInput" rows="10" placeholder="Paste your JSON here"></textarea>
<button onclick="visualizeJSON()">Visualize</button>
<div id="visualization"></div>
</div>
<script>
function visualizeJSON() {
const jsonInput = document.getElementById('jsonInput').value;
const visualizationDiv = document.getElementById('visualization');
try {
const jsonData = JSON.parse(jsonInput);
visualizationDiv.innerHTML = '';
visualizeData(jsonData, visualizationDiv);
} catch (error) {
visualizationDiv.innerHTML = `<p style="color: red;">Error: ${error.message}</p>`;
}
}
function visualizeData(data, parentElement) {
if (typeof data === 'object') {
if (Array.isArray(data)) {
const listElement = document.createElement('ul');
parentElement.appendChild(listElement);
for (const item of data) {
const listItem = document.createElement('li');
listElement.appendChild(listItem);
visualizeData(item, listItem);
}
} else {
const objectElement = document.createElement('ul');
parentElement.appendChild(objectElement);
for (const key in data) {
const propertyElement = document.createElement('li');
propertyElement.innerHTML = `<strong>${key}:</strong> `;
objectElement.appendChild(propertyElement);
visualizeData(data[key], propertyElement);
}
}
} else {
const textElement = document.createElement('span');
textElement.textContent = data;
parentElement.appendChild(textElement);
}
}
</script>
</body>
<div class="badge">
<p><a href="https://chat.openai.com/" target="_blank">
<img src="https://img.shields.io/badge/Co_authored%20by-ChatPT-9cf?logo=openai&logoColor=white"
alt="Coauthored by ChatGPT"></a></p>
<div id="copyright" style="font-family: 'Courier New', Courier, monospace;">
<b>© 2023 Trevor M. Tomesh</b>
</div>
</div>
</html>