|
| 1 | +## ⚑ JSON: |
| 2 | +JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for developer to read and write, and easy for machines to parse and generate. It is often used to transmit data between a server and a client web application, or between different parts of a web application. |
| 3 | + |
| 4 | +### ☴ Overview: |
| 5 | +1. [Parsing and Manipulating](#-parsing-and-manipulating) |
| 6 | + |
| 7 | +### ✦ Parsing and Manipulating: |
| 8 | +JSON syntax is based on JavaScript object literals. It consists of key-value pairs. which is enclose with curly braces {} for objects, and square brackets [] for arrays. |
| 9 | + |
| 10 | +*Syntax:* |
| 11 | +```json |
| 12 | +{ |
| 13 | + "name": "Kumar", |
| 14 | + "age": 30, |
| 15 | + "city": "Chennai" |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +**Methods Involved and Used on JSON:** |
| 20 | +It has built-in methods, which is provided in JavaScript to work with JSON: |
| 21 | +- `JSON.parse()`: Parses a JSON string into a JavaScript object. |
| 22 | +- `JSON.stringify()`: Converts a JavaScript object into a JSON string. |
| 23 | + |
| 24 | +**Parsing:** |
| 25 | +```javascript |
| 26 | +let jsonString = '{"name": "Kumar", "age": 30}'; |
| 27 | +let jsonObject = JSON.parse(jsonString); |
| 28 | +console.log(jsonObject.name); // Output: Kumar |
| 29 | +``` |
| 30 | + |
| 31 | +**Manipulating:** |
| 32 | +```javascript |
| 33 | +jsonObject.age = 31; |
| 34 | +jsonObject.city = "Chennai"; |
| 35 | +``` |
| 36 | + |
| 37 | +**Convert back into JSON:** |
| 38 | +```javascript |
| 39 | +let newJsonString = JSON.stringify(jsonObject); |
| 40 | +console.log(newJsonString); |
| 41 | +``` |
| 42 | + |
| 43 | +**Usages of JSON:** |
| 44 | +- *Data exchange:* It is commonly used to transmit data between a server and client. |
| 45 | +- *Data storage:* It can be used to store data locally in the browser using localStorage or sessionStorage. |
| 46 | +- *Configuration files:* It is often used for configuration files due to its human-readable format and easy for machine to parsing. |
| 47 | +- *API responses:* Most of APIs return data in JSON format. |
| 48 | + |
| 49 | +--- |
| 50 | +[⇪ To Top](#-json) |
| 51 | + |
| 52 | +[❮ Previous Topic](./regular-expressions.md)   [Next Topic ❯](./web-apis.md) |
| 53 | + |
| 54 | +[⌂ Goto Home Page](../README.md) |
0 commit comments