-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_JSON.js
More file actions
43 lines (34 loc) · 1.24 KB
/
Copy path09_JSON.js
File metadata and controls
43 lines (34 loc) · 1.24 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
//JSON - JavaScript Object Notation
//JSON is a format for storing and transporting data.
//JSON is often used when data is sent from a server to a web page.
//JSON is a lightweight data interchange format.
//JSON is language independent *
//JSON is "self-describing" and easy to understand
// {
// "name": "John",
// "age": 30,
// "email": "john@example.com",
// "address": {
// "city": "New York",
// "state": "NY"}
// }
//JSON.stringify()
//The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
//create a object
const person = {
name: "John",
age: 30,
email: "john@example.com",
address: {
city: "New York",
state: "NY"}
}
console.log(person);//print the object
console.log(JSON.stringify(person));//converts object to JSON string
//or
const jsonString = JSON.stringify(person);
console.log(jsonString);
//JSON.parse()
//The JSON.parse() method parses a JSON string into the object or value it represents.
const parsedObject = JSON.parse(jsonString);
console.log(parsedObject);