-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_JSON.js
More file actions
76 lines (48 loc) · 1.86 KB
/
test_JSON.js
File metadata and controls
76 lines (48 loc) · 1.86 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Think of a restaurant 🍴. When you (the client/browser) want food,
* you don’t go into the kitchen and grab it directly. Instead,
* you tell the waiter (JavaScript) what you want. The waiter goes to the kitchen (server),
* which prepares the food (data). But instead of bringing the entire kitchen,
* the waiter just brings you the menu items you ordered in a neat, easy-to-read way. That “neat format” is JSON.
* It’s like a well-arranged plate of data that the browser can easily understand and display on the webpage.
In short: JSON is the language (menu card) used to pass data between the client and server so that
the webpage can show meaningful information to you.
*
*
*
*/
// Converting object to stringfy
const person = {
name : 'devnet',
skills : ['devops' , 'networking', 'ci/cd'],
status : 'Engineer',
Designation : 'Infrustructer Engineer'
}
const mainperson = JSON.stringify(person)
console.log(mainperson);
const student = {
name: "Alice",
age: 22,
course: "Networking"
};
const jsonData = JSON.stringify(student);
console.log(jsonData);
// converting string to object
const products = [
{ id: 1, name: "Laptop", price: 1200 },
{ id: 2, name: "Mouse", price: 25 }
];
const jsonProducts = JSON.stringify(products)
const jsonProducts2 = JSON.parse(jsonProducts)
console.log(jsonProducts2)
const serverResponse = '[{"name":"DevNet","role":"Engineer"},{"name":"John","role":"Admin"}]';
const serverResponse2 = JSON.stringify(serverResponse)
// console.log(serverResponse2);
const serverResponse3= JSON.parse(serverResponse2);
// fake api with json place holder
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(res => res.json())
.then(data => console.log(data))
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))