
문자열을 JSON으로서 구문 분석하고, 선택적으로 분석 결과의 값과 속성을 변환해 반환합니다.
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
console.log(obj);
// expected output : Object { result: true, count: 42 }
console.log(obj.count);
// expected output: 42
console.log(obj.result);
// expected output: true
var str = '{ "v" : 42 }';
console.log(JSON.parse(str)); // Object {v: 42}
str = '{ "n" : null }';
console.log(JSON.parse(str)); // Object {n: null}
str = '{ "x" : NaN }';
console.log(JSON.parse(str)); // Uncaught SyntaxError: Unexpected token N
str = '{ "y" : Infinite }';
console.log(JSON.parse(str)); // Uncaught SyntaxError: Unexpected token I
주어진 값에 해당하는 JSON 문자열을 반환합니다. 선택 사항으로 특정 속성만 포함하거나 사용자 정의 방식으로 속성을 대체합니다.
console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"
console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)]));
// expected output: "[3,"false",false]"
console.log(JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] }));
// expected output: "{"x":[10,null,null,null]}"
console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// expected output: ""2006-01-02T15:04:05.000Z""
console.log(JSON.stringify(undefined)) // undefined
console.log(JSON.stringify(function(){})) // undefined
console.log(JSON.stringify(Symbol(''))) // undefined
console.log(JSON.stringify([undefined, function(){}, Symbol('')])) // [null, null, null]
console.log(JSON.stringify(NaN)) // null
console.log(JSON.stringify(null)) // null
console.log(JSON.stringify(Infinity)) // null
console.log(JSON.stringify([NaN, null, Infinity])) // [null, null, null]
TIL : 문자열을 JSON으로 변환할 때 NaN, Infinity는 지원하지 않는다.
Reference : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/JSON