-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_view.html
89 lines (85 loc) · 2.68 KB
/
json_view.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JSON Beautify Viewer</title>
<!-- Bootstrap CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<style>
/* CSS khusus untuk layout */
.code-editor {
width: 100%;
height: 150px;
font-family: monospace;
font-size: 14px;
}
.json-output {
border: 1px solid #ccc;
min-height: 150px;
padding: 10px;
margin-top: 10px;
background-color: #f8f9fa;
white-space: pre-wrap;
word-wrap: break-word;
overflow-x: auto;
font-family: monospace;
}
.json-output span {
display: inline-block;
padding: 0 2px;
}
.key { color: #d73a49; }
.string { color: #032f62; }
.number { color: #005cc5; }
.boolean { color: #22863a; }
.null { color: #6f42c1; }
</style>
</head>
<body>
<div class="container mt-5">
<h2 class="text-center">JSON Beautify Viewer</h2>
<div class="row mt-4">
<div class="col-md-12">
<h5>JSON Input</h5>
<textarea id="json-input" class="code-editor form-control" placeholder="Enter JSON data here"></textarea>
<button class="btn btn-primary mt-3" onclick="beautifyJson()">Beautify JSON</button>
</div>
</div>
<div class="row mt-4">
<div class="col-md-12">
<h5>JSON Output</h5>
<div id="json-output" class="json-output"></div>
</div>
</div>
</div>
<!-- JavaScript -->
<script>
function beautifyJson() {
const jsonInput = document.getElementById('json-input').value;
const jsonOutput = document.getElementById('json-output');
try {
const jsonObject = JSON.parse(jsonInput);
const formattedJson = JSON.stringify(jsonObject, null, 2); // Format JSON with indentation
jsonOutput.innerHTML = syntaxHighlight(formattedJson);
jsonOutput.style.color = 'black';
} catch (e) {
jsonOutput.textContent = 'Invalid JSON data. Please check your input.';
jsonOutput.style.color = 'red';
}
}
function syntaxHighlight(json) {
const jsonString = json
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"(.*?)":/g, '<span class="key">"$1":</span>')
.replace(/"(.*?)"/g, '<span class="string">"$1"</span>')
.replace(/(\d+)/g, '<span class="number">$1</span>')
.replace(/(true|false)/g, '<span class="boolean">$1</span>')
.replace(/(null)/g, '<span class="null">$1</span>');
return jsonString;
}
</script>
</body>
</html>