JSON to Text Tool
/* Reset default margins/paddings */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
header {
text-align: center;
margin-bottom: 20px;
}
header h1 {
color: #0073e6;
}
header p {
font-size: 1.1em;
color: #555;
}
.content {
display: flex;
flex-direction: column;
}
.input-section, .output-section {
margin-bottom: 20px;
}
textarea {
width: 100%;
padding: 10px;
font-size: 1em;
border: 1px solid #ccc;
border-radius: 5px;
resize: vertical;
}
textarea:focus {
border-color: #0073e6;
outline: none;
}
button {
padding: 10px 20px;
background-color: #0073e6;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1.1em;
transition: background-color 0.3s;
}
button:hover {
background-color: #005bb5;
}
.error-message {
color: #d9534f;
font-size: 1em;
margin-top: 10px;
}
.output-text {
background-color: #f9f9f9;
padding: 10px;
border-radius: 5px;
font-family: 'Courier New', monospace;
white-space: pre-wrap;
word-wrap: break-word;
border: 1px solid #ccc;
}
footer {
text-align: center;
margin-top: 20px;
font-size: 0.9em;
color: #555;
}
footer a {
color: #0073e6;
text-decoration: none;
}
footer a:hover {
text-decoration: underline;
}
document.getElementById("convertBtn").addEventListener("click", function () {
const jsonInput = document.getElementById("jsonInput").value;
const outputText = document.getElementById("outputText");
const errorMessage = document.getElementById("errorMessage");
try {
// Validate and parse the JSON input using JSONLint
const jsonObject = JSON.parse(jsonInput);
// Reset previous error messages
errorMessage.textContent = '';
// Convert JSON to a formatted text output
const textOutput = formatJsonToText(jsonObject);
outputText.textContent = textOutput;
} catch (e) {
// Display error message if invalid JSON
errorMessage.textContent = 'Invalid JSON input. Please check your syntax.';
outputText.textContent = ''; // Clear the previous output
}
});
function formatJsonToText(jsonObject) {
let text = '';
// Iterate over each key-value pair in the JSON object
for (const [key, value] of Object.entries(jsonObject)) {
text += `${key}: ${value}\n`;
}
return text;
}