[JavaScript] ๐Ÿ›’JSON, ์–ด๋””๊นŒ์ง€ ์•Œ์•„๋ณด๊ณ  ์˜ค์…จ์–ด์š”?

Dongjun Kimยท2022๋…„ 12์›” 5์ผ
2

โš ๏ธJavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
1/4
post-thumbnail

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹œ๋ฆฌ์ฆˆ:

[JavaScript] ๐Ÿ›’JSON, ์–ด๋””๊นŒ์ง€ ์•Œ์•„๋ณด๊ณ  ์˜ค์…จ์–ด์š”?
[JavaScript] ๋™๊ธฐ์ ์ธ(Synchronous) ์ฝœ๋ฐฑ(callback)ํ•จ์ˆ˜๋„ ์žˆ์„๊นŒ?
[JavaScript] ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ž์—๊ฒŒ ๋ณต์ด ์žˆ๋‚˜๋‹ˆ, Promise
[JavaScript] ๋น„๋™๊ธฐ ์ด๊ธฐ๋Š” ๋ฒ•, Async์™€ Await.


JSON, ์–ด๋””๊นŒ์ง€ ์•Œ์•„๋ณด๊ณ  ์˜ค์…จ์–ด์š”?

JSON. ์›น๊ฐœ๋ฐœ์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ๋งŽ์ด๋„ ๋ณด๊ณ  ๋“ค์—ˆ์„ ๊ฑฐ๋‹ค. ์ •๋ง ์–ด๋”œ๊ฐ€๋“  ๋ณด์ด๋Š” JSON, ๊ทธ๊ฒŒ ๋„๋Œ€์ฒด ๋ญ˜๊นŒ?


์ด๋ฆ„์ด ์–ด๋–ป๊ฒŒ JSON?

JSON์€ JavaScript Object Notation์˜ ์ค„์ž„๋ง์ด๋‹ค. Notation, ๊ทธ๋‹ˆ๊นŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ Object ํƒ€์ž…์„ ๋‹ฎ์€ ํ•˜๋‚˜์˜ ํ˜•์‹์ด๋‹ค. ๋‹ฎ์•˜์œผ๋‹ˆ๊นŒ ๋ณ€ํ™˜ํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค.


ํ˜•์‹์ธ๋ฐ ์™œ ๊ทธ๋ ‡๊ฒŒ ์ค‘์š”ํ•˜์ง€?

๋˜๊ฒŒ ์œ ์šฉํ•œ ํ˜•์‹์ด๋ผ๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค. ์ธ๊ฐ„์ด ์ฝ๊ฑฐ๋‚˜ ์“ฐ๊ธฐ ์‰ฝ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ๋„ ์‰ฝ๋‹ค.
์‰ฝ๊ฒŒ ๋งํ•ด ์ •๋ณด๋ฅผ ๋‹ด๊ธฐ ์‰ฝ๊ณ , ์ „์†กํ•˜๊ธฐ๋„ ํŽธํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์˜จ๊ฐ– ๊ณณ์— ๋‹ค ์“ฐ์ธ๋‹ค.


๋‹ค์Œ์€ ํŒŒํŒŒ๊ณ  ๋ฒˆ์—ญ API์˜ ์‘๋‹ต ์˜ˆ์‹œ๋‹ค.

{
    "message": {
        "@type": "response",
        "@service": "naverservice.nmt.proxy",
        "@version": "1.0.0",
        "result": {
            "srcLangType":"ko",
            "tarLangType":"en",
            "translatedText": "tea"
        }
    }
}

๋ฒˆ์—ญํ•ด๋‹ฌ๋ผ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์œ„์™€๊ฐ™์€ ์‘๋‹ต์ด ๋Œ์•„์˜จ๋‹ค.
๊ทธ๋ ‡๋‹ค. JSON ๊ทธ ๋…€์„์ด๋‹ค.


๋‹ค์Œ์€ npm์˜ package.json์ด๋‹ค.

> npm init --yes
Wrote to /home/monatheoctocat/my_package/package.json:

{
  "name": "my_package",
  "description": "",
  "version": "1.0.0",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/monatheoctocat/my_package.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/monatheoctocat/my_package/issues"
  },
  "homepage": "https://github.com/monatheoctocat/my_package"
}

JSON์— ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์‰ฝ๊ฒŒ ๋‹ด์„ ์ˆ˜ ์žˆ๊ณ , JSON์œผ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•ด์˜ค๊ธฐ๋„ ์‰ฌ์›Œ ๋‹ค์–‘ํ•œ ๊ณณ์— ๋งŽ์ด ์“ฐ์ธ๋‹ค.


๊ทธ๋ž˜์„œ ์ง„์งœ ๋ญ”๋ฐ?

JSON์ด ๋ญ๋ƒ๊ณ ? JSON์€ string ํ˜•ํƒœ๋กœ ์กด์žฌํ•œ๋‹ค.
๊ทธ๋‹ˆ๊นŒ JavaScript์˜ Object์˜ ํ˜•์‹์„ ๋นŒ๋ฆฐ string์ผ ๋ฟ์ด๋‹ค. ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” string ํ˜•ํƒœ์—ฌ์•ผ ํ•œ๋‹ค.
(์•„..์•Œ๊ฒ ๋‹ค..!)

์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๋ฐ?

๊ทธ๋Ÿผ ์ด string์„ ์–ด๋””์„ ๊ฐ€ ๋ฐ›์•„์™”๋‹ค๊ณ  ์น˜์ž. ์ด๊ฑธ๋กœ ๋ญ˜ ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” string์ด๋‹ˆ๊นŒ ์ •๋ณด์— ์ ‘๊ทผํ•ด์•ผ ํ•  ๊ฑฐ๋‹ค. ์ •๋ณด์— ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๋งค๋ฒˆ console.log๋กœ ์ฐ์–ด์„œ ๋ˆˆ์œผ๋กœ ํ™•์ธํ•ด์•ผ ํ• ๊นŒ?
์•„๋‹ˆ๋ฉด string์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋Œ๋ฉด์„œ key์™€ value๋ฅผ ๊ตฌ๋ถ„ํ•ด๋‚ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฑฐ๋‹ค.

๋‹คํ–‰ํžˆ ์ด string์€ Object๊ณผ ๋‹ฎ์•˜๊ธฐ ๋•Œ๋ฌธ์— Object๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค.


JSON๊ณผ JavaScript

JavaScript๋Š” JSON Object๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
๋งค๋ฒˆ ๋ถˆ๋Ÿฌ์„œ ๋งŒ๋“ค๊ฑฐ๋‚˜ ํ•˜๋Š” Object๋Š” ์•„๋‹ˆ๋‹ค.


JavaScript์—์„œ๋Š” ๋žœ๋คํ•œ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ณ  ์‹ถ์„ ๋•Œ, ์•„๋ž˜์ฒ˜๋Ÿผ Math.random()์„ ์“ด๋‹ค.

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

0๋ถ€ํ„ฐ max-1๊นŒ์ง€ ์ค‘์—์„œ ๋žœ๋คํ•œ ์ˆ˜๋ฅผ ๊ณจ๋ผ์ฃผ๋Š” ํ•จ์ˆ˜๋‹ค.
์–ด์จŒ๋“ , ์ด๋•Œ ์“ฐ๋Š” Math Object๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒŒ JSON Object์ด๋‹ค.


JSON string์„ Objectํ˜•ํƒœ๋กœ ์ฝ์–ด์˜ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” JSON Object ์•ˆ์— ๋‚ด์žฅ๋œ method์ธ parse()๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(typeof obj);
// output: "object"

json์ด๋ผ๋Š” string์„ ๋งŒ๋“ค์–ด์„œ JSON.parse()์— ๋„˜๊ฒจ์คฌ๋”๋‹ˆ Object๊ฐ€ ๋˜์—ˆ๋‹ค!


๋ฐ˜๋Œ€๋กœ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” Object๋ฅผ ์–ด๋”˜๊ฐ€์— ์ „์†กํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด string์œผ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค. ์ด๋•Œ๋Š” JSON.stringfy()๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

console.log(JSON.stringify({ x: 5, y: 6 }));
// expected output: "{"x":5,"y":6}"

Object๊ฐ€ string์ด ๋˜์—ˆ๋‹ค!


ํ˜ธ๊ธฐ์‹ฌ ํ•ด๊ฒฐ!

์–ด๋””๋“ ์ง€ ์“ฐ์ด๋Š” JSON์— ๋Œ€ํ•ด ์•Œ์•„๋ดค๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต์ง„ ์•Š์ง€๋งŒ, ์›น๊ฐœ๋ฐœ์—์„œ ํ•ญ์ƒ ๋“ฑ์žฅํ•˜๋Š” ๊ฐœ๋…์ธ๋งŒํผ, ๋งŽ์ด ์จ์„œ ์นœ์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜๋‹ค.

๐Ÿฐ๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” JSON๊ณผ API๋ฅผ ํ™œ์šฉํ•œ ๊ฐ„๋‹จํ•œ ํŠœํ† ๋ฆฌ์–ผ์„ ๋งŒ๋“ค์–ด๋ณผ๊นŒ ์ƒ๊ฐ ์ค‘์ด๋‹ค.๐Ÿง‘๐Ÿปโ€๐Ÿณ


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹œ๋ฆฌ์ฆˆ:

[JavaScript] ๐Ÿ›’JSON, ์–ด๋””๊นŒ์ง€ ์•Œ์•„๋ณด๊ณ  ์˜ค์…จ์–ด์š”?
[JavaScript] ๋™๊ธฐ์ ์ธ(Synchronous) ์ฝœ๋ฐฑ(callback)ํ•จ์ˆ˜๋„ ์žˆ์„๊นŒ?
[JavaScript] ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ž์—๊ฒŒ ๋ณต์ด ์žˆ๋‚˜๋‹ˆ, Promise
[JavaScript] ๋น„๋™๊ธฐ ์ด๊ธฐ๋Š” ๋ฒ•, Async์™€ Await.


์ฐธ๊ณ 
mdn docs

profile
์ค‘์š”ํ•œ ๊ฒƒ์€ ๊บพ์ด์ง€ ์•Š๋Š” ๋งˆ์Œโ˜•๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€