๋ฉฐ์น ์ ๋๋ฃ์ ์ฝ๋๋ฅผ ๋ณด๋ค๊ฐ ๋ฐ๋ณด๊ฐ์ ์ง๋ฌธ์ ํ๊ฒ๋์ด ๋ค์ ์ ๋ฆฌํด๋ณด๋ JSON์ ๊ฐ๋
์๋ฒ์ ์์ฒญ์ ๋ณด๋ด๋ ๋ฐ๋๋ฅผ `JSON.stringify()`๋ก ๋ณํํ๋ ์ฝ๋๋ฅผ ๋ณด๋๋ฐ ๊ฐ์๊ธฐ ํท๊ฐ๋ ธ๋ค. stringify๋ ๋ง๊ทธ๋๋ก ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ์ง๋ ฌํ์์ผ์ฃผ๋ ๊ฒ์๋๋๊ฐ? ์ ์๋ฒ์ ์์ฒญ๋ณด๋ผ๋ stringify๋ฅผ ์ํค๋๊ฑฐ์ง..??
๋๋ .. JSON๊ณผ JavaScript์ object๋ฅผ ํท๊ฐ๋ฆฌ๊ณ ์์๋ ๊ฒ์ด์๋ค.. ๐ฑ
๋จผ์ JSON์ด ๋ญ์ง๋ฅผ ์ ํํ ์์์ผํ๋ค.
JSON
JSON(JavaScript Object Notation)์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์์ ์๋ ์ผ๋ฐ ํ ์คํธ ํฌ๋งท์ ๋ปํ๋ค.
JavaScript์ ๊ฐ์ฒด ํ๊ธฐ๋ฒ์ ๋ณธ๋ฐ์ JSON syntax๊ฐ ๋ง๋ค์ด์ก๋ค.
JSON ํฌ๋งท์ ํ ์คํธ์ผ๋ฟ JavaScript์ ๊ฐ์ ํ๋ก๊ทธ๋๋ฐ์ธ์ด์์ ์ฌ์ฉํ๋ ค๋ฉด ๋ณํ์ด ํ์ํ๋ค.
'{"name":"John", "age":30, "car":null}'
JSON.stringify()
JavaScript ๊ฐ์ด๋ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์์ด๋ก ๋ณํํ ๋ ์ฌ์ฉํ๋ค.
console.log(JSON.stringify({ x: 5, y: 6 }));
// Expected output: '{"x":5,"y":6}'
console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol('')] }));
// Expected output: '{"x":[10,null,null,null]}'
JSON.parse()
JSON๋ฌธ์์ด์ JavaScript์ ๊ฐ์ฒด๋ ๊ฐ์ผ๋ก ํ์ฑํ๊ณ ์ถ์๋ ์ฌ์ฉํ๋ค.
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
console.log(obj);
// Expected output: Object { result: true, count: 42 }
json()
๊ทธ๋ ๋ค๋ฉด ํํ fetch api๋ฅผ ํธ์ถํด์ ๋ฐ์์ค๋ response์ ์ ์ฉํ๋ `json()` ๋ฉ์๋๋ ๋ฌด์์ผ๊น
๋ฉ์๋ ์ด๋ฆ์ผ๋ก ๋ณด์ ๊ฒฐ๊ณผ๊ฐ์ผ๋ก json ํ์์ ๋ฐํํด์ค ๊ฒ ๊ฐ์ง๋ง, ๊ทธ๋ ์ง์๋ค.
json() ๋ฉ์๋์ ์ญํ ์ JSON ๊ฐ์ ์ ๋ ฅ๋ฐ์ ํ์ฑํ์ฌ JavaScript ๊ฐ์ฒด๋ฅผ ์์ฑํด์ฃผ๊ณ , ์ด ๊ฐ์ฒด๋ฅผ ํ๋ก๋ฏธ์ค๋ก ๊ฐ์ธ์ ๋ฐํํด์ค๋ค.
fetch('https://api.realworld.io/api/articles')
.then((res) => {
console.log(res, 'res ์ฝ์');
console.log(res.json(), 'res.json() ์ฝ์');
})
`Response` ๊ฐ์ฒด์ `json()` ๋ฉ์๋์ ์๋๋ฐฉ์
fetch API๋ฅผ ์ฌ์ฉํด์ ์๋ฒ์์ ์๋ต๋ฐ์ response๋ฅผ ๊ทธ๋๋ก ์ฝ์์ ์ฐ์ด๋ณด๋ฉด ์๋์ ๊ฐ์ด `Response` ๊ฐ์ฒด๊ฐ ์ฐํ๋ ๊ฒ์ ํ์ธํ ์๊ฐ ์๋ค.
์ด๋ `Response` ๊ฐ์ฒด ๋ด๋ถ์ ์๋ต ๋ฐ์ดํฐ์ธ `body` ํ๋๋ฅผ ํผ์ณ๋ณด๋ฉด body ์๋ ReadableStream์ด ๋ด๊ฒจ์๋๊ฑธ ๋ณผ์์๋ค.
์๋ฒ๋ก๋ถํฐ ์๋ต๋ฐ์ ๋ฐ์ดํฐ๋ ์์ง ๋ค ์ฝํ์ง ์ํ๊ฐ ์๋๊ณ , ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์กํ๊ธฐ์ํด ์กฐ๊ฐ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ์คํธ๋ฆผ ํํ๋ก ์กด์ฌํ๋ ์ํ์ด๋ค. ์คํธ๋ฆผ์ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์กํ๋ ๋ฐฉ์์ด๋ค.
์ด Stream ํํ์ ์๋ต๊ฐ์ `json()` ๋ฉ์๋์ ์ฌ์ฉํ๋ค๋ฉด, ๋ด๋ถ์ ์ผ๋ก ์คํธ๋ฆผ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ฝ์ ํ, ๋ฌธ์์ด๋ก ๋ณํํ ๋ค์์ ๊ทธ ๋ฌธ์์ด์ JSONํ์๋ฅผ ํตํด JavaScript ๊ฐ์ฒด๋ก ๋ณํํ๋ค.
๊ทผ๋ฐ ์ด ๊ณผ์ ์ ํ๋ฒ์ JavaScript ๊ฐ์ฒด๋ก ๋ณํ๋๋๊ฒ ์๋๊ณ , ๋น๋๊ธฐ์ ์ผ๋ก ์ผ์ด๋๊ธฐ๋๋ฌธ์ json() ๋ฉ์๋๋ Promise๋ฅผ ๋ฐํํ๋ค.
์ฆ, body์ ๋ด๊ฒจ์๋ ReadableStream ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด javaScript์ object๋ก ๋ณํํด์ฃผ๋ ๊ฒฐ๊ณผ๊ฐ์ ํ๋ผ๋ฏธ์ค์ ๊ฐ์ผ์ฑ๋ก ๋ฐํํด์ฃผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
Refs
https://www.json.org/json-en.html
JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition
www.json.org
https://www.w3schools.com/js/js_json_intro.asp
W3Schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
https://developer.mozilla.org/en-US/docs/Web/API/Response/json
Response: json() method - Web APIs | MDN
The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON.
developer.mozilla.org
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
js get object values from string path (0) | 2025.05.26 |
---|---|
api ์์ฐจ์ ์คํ | catch (err) vs catch | throw error vs return error (0) | 2024.05.22 |
TypeScript index signature ์ธ๋ฑ์ค ์๊ทธ๋์ณ (0) | 2023.09.08 |
JS ๋ฐฐ์ด ๊ฐ์ฒด์์ ์์ immutable, mutable (0) | 2023.06.26 |
JS parseInt() vs Number() (0) | 2022.11.13 |