본문으로 건너뛰기

JSON (JavaScript Object Notation)

JSON (JavaScript Object Notation)

JSON is a data exchange format used for transmitting information. It is widely used for data interaction between servers and web pages.


Basic Structure of JSON

JSON is characterized by being represented only in the form of objects or arrays.

This characteristic ensures that the structure of the data is clearly expressed when transmitting data.


  1. Object: A collection of key-value pairs enclosed in curly braces { }.
{
"key1": "value1",
"key2": "value2",
...
}
JSON Example
{
"name": "Hong Gildong",
"age": 25
}

  1. Array: A list of values enclosed in square brackets [ ].
["apple", "banana", "cherry"]

Handling JSON in JavaScript

JavaScript has a built-in JSON object for working with JSON.

  1. JSON.stringify(): Converts JavaScript objects or values into a string.
let obj = {
name: 'Hong Gildong',
age: 25,
};
let jsonString = JSON.stringify(obj);
console.log(jsonString); // Outputs '{"name":"Hong Gildong","age":25}'

  1. JSON.parse(): Converts a JSON string into a JavaScript object or value.
let jsonString = '{"name":"Hong Gildong","age":25}';
let obj = JSON.parse(jsonString);
console.log(obj.name); // Outputs "Hong Gildong"

Points to Note

  • In JSON, property names must always be surrounded by double quotes ("). While JavaScript allows single quotes ('), JSON does not permit them.

  • Data types not supported by JSON (e.g., functions, undefined) cannot be converted into JSON.