<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Viewer</title>
<link rel="stylesheet" href="styles.css">
<script defer src="script.js"></script>
</head>
<body>
<div class="container">
<h1>JSON Viewer</h1>
<textarea id="jsonInput" placeholder="Paste your JSON here..."></textarea>
<div class="buttons">
<button onclick="formatJSON()">Format JSON</button>
<button onclick="clearJSON()">Clear</button>
<button id="copyBtn">📋 Copy</button>
</div>
<pre id="jsonOutput" class="output"></pre>
<p id="errorMsg"></p>
</div>
</body>
</html>
/* Google Font */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
background: linear-gradient(135deg, #1E3C72, #2A5298);
color: white;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
text-align: center;
padding: 20px;
}
.container {
background: rgba(255, 255, 255, 0.2);
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
max-width: 600px;
width: 100%;
}
h1 {
margin-bottom: 20px;
font-size: 24px;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
border-radius: 5px;
border: none;
outline: none;
font-size: 16px;
background: #2C3E50;
color: #ecf0f1;
resize: none;
}
.buttons {
display: flex;
justify-content: space-between;
margin: 15px 0;
}
button {
padding: 10px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
flex: 1;
margin: 0 5px;
}
button:hover {
opacity: 0.8;
}
/* Button Colors */
button:nth-child(1) {
background: #3498db;
color: white;
}
button:nth-child(2) {
background: #e74c3c;
color: white;
}
#copyBtn {
background: #2ecc71;
color: white;
}
.output {
background: #1A1A1A;
padding: 10px;
border-radius: 5px;
white-space: pre-wrap;
text-align: left;
color: #ffffff;
font-size: 14px;
max-height: 300px;
overflow-y: auto;
}
/* JSON Syntax Highlighting */
.key { color: #f1c40f; }
.string { color: #2ecc71; }
.number { color: #e67e22; }
.boolean { color: #9b59b6; }
.null { color: #e74c3c; }
#errorMsg {
color: yellow;
font-size: 14px;
margin-top: 10px;
}
document.getElementById('copyBtn').addEventListener('click', copyToClipboard);
function formatJSON() {
let jsonInput = document.getElementById('jsonInput').value.trim();
let jsonOutput = document.getElementById('jsonOutput');
let errorMsg = document.getElementById('errorMsg');
try {
let parsedJSON = JSON.parse(jsonInput);
let formattedJSON = JSON.stringify(parsedJSON, null, 4);
jsonOutput.innerHTML = syntaxHighlight(formattedJSON);
errorMsg.textContent = "";
} catch (error) {
errorMsg.textContent = "Invalid JSON format!";
jsonOutput.textContent = "";
}
}
function clearJSON() {
document.getElementById('jsonInput').value = "";
document.getElementById('jsonOutput').textContent = "";
document.getElementById('errorMsg').textContent = "";
}
function copyToClipboard() {
let jsonOutput = document.getElementById('jsonOutput').innerText;
if (jsonOutput === '') {
alert("Nothing to copy!");
return;
}
navigator.clipboard.writeText(jsonOutput).then(() => {
alert("Copied to Clipboard!");
});
}
/* Syntax Highlighting */
function syntaxHighlight(json) {
json = json.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^"\\])*")|(\b(true|false|null)\b)|(\b-?\d+(\.\d+)?([eE][+-]?\d+)?\b)/g, function(match) {
let cls = "number";
if (/^"/.test(match)) {
cls = /:$/.test(match) ? "key" : "string";
} else if (/true|false/.test(match)) {
cls = "boolean";
} else if (/null/.test(match)) {
cls = "null";
}
return `<span class="${cls}">${match}</span>`;
});
}
