-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path043-JSON.js
64 lines (47 loc) · 2.13 KB
/
043-JSON.js
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
/*
author : Jaydatt Patel
JavaScript Object Notation (JSON):
The modern web consists of millions and millions of web pages, connected services and databases.
For example, if the stringified JSON data was converted to an object that had the following structure:
const ObjectOfJSON = {
USD: 'str',
GBP: [],
EUR: {}
}
const ArrayOfJSON = [
{ USD: 'str' },
{ GBP: [] },
{ EUR: {} }
]
JSON is just a string - but there are rules that it must follow :
- JSON is a string, but it must be a properly-formatted string. In other words, it must adhere to some rules.
- If a JSON string is not properly formatted, JavaScript would not be able to parse it into a JavaScript object.
- JSON can work with some primitives and some complex data types, as described below.
- Only a subset of values in JavaScript can be properly stringified to JSON and parsed from a JavaScript object into a JSON string.
These values include:
- primitive values: strings, numbers, bolleans, null
- complex values: objects and arrays (no functions!)
- Objects have double-quoted strings for all keys
- Properties are comma-delimited both in JSON objects and in JSON arrays, just like in regular JavaScript code
String properties must be surrounded in double quotes. For example:
"fruits", "vegetables"
Number properties are represented using the regular JavaScript number syntax; e.g
5, 10, 1.2
Boolean properties are represented using the regular JavaScript boolean syntax, that is:
true, false
Null as a property is the same as in regular JavaScript; it's just a null
*/
const obj = {
name: "Rahul",
sub: ["eng", "math"],
info: {
email: "[email protected]",
contact: "+12345",
},
};
// JSON.stringify(obj) to make object to JSON string format
const Jstr = JSON.stringify(obj);
console.log("JSON String format : ", Jstr);
//JSON.parse(Jstr) to conert JSON string into object.
const obj2 = JSON.parse(Jstr);
console.log("Object : ", obj2);