이를 위해 JSON.stringify()와 함께 JSON.parse()를 사용합니다.
예시
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/4.7.0/css/font-awesome.min.css">
</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> 위의 프로그램을 실행하려면 anyName.html(index.html)이라는 파일명을 저장하고 파일을 우클릭하여 VS Code 편집기에서 라이브 서버로 열기 옵션을 선택하면 됩니다.
출력
이것은 다음과 같은 출력을 생성합니다 -

이제 잘못된(형식화되지 않은) JSON을 넣을 것입니다. 샘플 JSON 데이터는 다음과 같습니다 -

버튼을 클릭하면 다음과 같은 샘플 출력이 표시됩니다. 예를 들어 올바른 형식의 JSON −
