उदाहरण
JSON.parse() और JSON.stringify() का उपयोग करके अनफॉर्मेटेड JSON को प्रीटिफाई करें।
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea id="prettyJSONFormat" cols="100" rows="20"></textarea>
<button onclick="printTheJSONInPrettyFormat()">Click The Button To get the Pretty JSON</button>
<script>
function printTheJSONInPrettyFormat() {
var badJSON = document.getElementById('prettyJSONFormat').value;
var parseJSON = JSON.parse(badJSON);
var JSONInPrettyFormat = JSON.stringify(parseJSON, undefined, 4);
document.getElementById('prettyJSONFormat').value = JSONInPrettyFormat;
}
</script>
</body>
</html>
फ़ाइल सेव करें और Live Server से खोलें।
आउटपुट
बटन क्लिक करने पर टेक्स्टएरिया में JSON प्रॉपरली फॉर्मेटेड दिखाई देगा।