FrontEnd/Javascript

[Javascript] Json 데이터 예쁘게 출력 (JSON.stringify pretty print)

J.BF 2022. 7. 31. 23:50

JSON.stringify 함수를 사용하여 Json 데이터를 예쁘게 출력할 수 있다.

const jsonData = {
    "test_int": 12,
    "test_float": 1.23,
    "test_str": "asdf",
    "test_array": [1, 2, 3, 4],
    "test_object": {
        "sub_test": 123,
        "sub_test2": "qwer",
    },
};

console.log(JSON.stringify(jsonData)); // not pretty print
console.log(JSON.stringify(jsonData, null, 2)); // pretty print (space:2)