Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 자바스크립트 딥다이브
- peerdependencies
- 자바스크립트 패턴
- 올림픽 통계 서비스 최적화
- 블로그 서비스 최적화
- 모던 자바스크립트 Deep Dive
- 빌트인 객체
- Set과 Map
- 전역변수의문제점
- 프로퍼티 어트리뷰트
- 이미지 갤러리 최적화
- 프로그래머스
- const
- 비전공이지만 개발자로 먹고삽니다
- DOM
- 스코프
- Property Attribute
- 브라우저의 렌더링 과정
- 제너레이터와 async/await
- 딥다이브
- 자바스크립트
- 프론트엔드 성능 최적화 가이드
- ES6함수 추가기능
- 커리어
- var 사용금지
- Babel과 Webpack
- package management
- 인터넷 장비
- 디스트럭처링
- 이벤트
Archives
- Today
- Total
Dev Blog
What is JSON File ? 본문
JSON stands for JavaScript Object Notation. A JSON file has .json as its extension and the data inside are represented in a key:value pair, just like a traditional JavaScript object.
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": 7,
"key5": null,
"favFriends": ["Kolade", "Nithya", "Dammy", "Jack"],
"favPlayers": {"one": "Kante", "two": "Hazard", "three": "Didier"}
}
Accepted JSON Data Types
JSON can be defined in an object or an array, which might take in several objects.
So, objects and arrays are automatically acceptable data types in JSON. Other data types that it supports are boolean, null, and string.
Data types such as undefined, function, and date are not supported by JSON.
JSON syntax rules to know:
- All the data in the file must be surrounded by curly braces if you're representing it as an object, and in square brackets if it is an array.
- Single quotes are not allowed
- The key in each JSON must be unique and must be in double quotes
- Numbers must not be enclosed in double-quotes, otherwise they will be treated as strings.
- The null data type must not be enclosed in double-quotes.
- Boolean values can only be true or false.
- Each key:value pair must be terminated with a comma except for the last item
- A particular object inside an array must be terminated by a comma, too.
How JSON Data is Sent to the Client (Browser)
JSON was created out of the need to send data from the server (a database, for example) to the client (browsers) in real-time.
In JavaScript, JSON.parse() converts JSON data to objects and JSON.stringify() converts an object's key:value pair to JSON data.
<h2>Here is the Data from the JSON:</h2>
<div id="json"></div>
const JSONData = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": 7,
"key5": null,
"favFriends": ["Kolade", "Nithya", "Dammy", "Jack"],
"favPlayers": {"one": "Kante", "two": "Hazard", "three": "Didier"}
}
const JSONString = JSON.stringify(JSONData)
const JSONDisplay = document.querySelector("#json")
JSONDisplay.innerHTML = JSONString
JSONString: String type
const JSONData =
'{"name": "Kolade", "favFriends": ["Kolade", "Nithya", "Rocco", "Jack"], "from": "Africa"}';
try {
const JSONString = JSON.parse(JSONData);
const JSONDisplay = document.querySelector("#json");
JSONDisplay.innerHTML = JSONString.name + ", [" + JSONString.favFriends + "], " + JSONString.from;
} catch (error) {
console.log("Cannot parse the JSON Data");
}
JSONString: Object Type
Why use JSON Type ?
JSON (JavaScript Object Notation) is most widely used data format for data interchange on the web. JSON is a lightweight text based, data-interchange format and it completely language independent. It is based on a subset of the JavaScript programming language and it is easy to understand and generate.
We use JSON because it's extremely lightweight to send back and forth in HTTP requests and responses due to the small file size. It's easy to read compared to something like XML since it's much cleaner and there's not as many opening and closing tags to worry about.
Source from
- https://www.freecodecamp.org/news/what-is-a-json-file-example-javascript-code/
'Web Development > General Tech Knowledges' 카테고리의 다른 글
DOM 트리의 탐색 (0) | 2022.12.13 |
---|---|
dependencies version in package.json (0) | 2021.10.16 |
Service worker (0) | 2021.09.03 |
CDN (0) | 2021.08.01 |
PWA (0) | 2021.08.01 |
Comments