-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_JSON.js
24 lines (17 loc) · 876 Bytes
/
06_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
// from JSON to string ====> stringify
// from string to JSON ====> parse
function jsonMethods(jsonString) {
console.log("Original JSON String:", jsonString);
// Parsing JSON string to JavaScript object
let parsedObject = JSON.parse(jsonString);
console.log("After JSON.parse():", parsedObject);
// Stringifying JavaScript object to JSON string
let jsonStringified = JSON.stringify(parsedObject);
console.log("After JSON.stringify():", jsonStringified);
}
// Example Usage for JSON Methods
const sampleJSONString = '{"key": "value", "number": 42, "nested": {"nestedKey": "nestedValue"}}';
jsonMethods(sampleJSONString);
// Any time we wnat to share a json data. we do share it in string so the types and formatting does not go away.
// Important.
// for that we do use parse method to convert js to string. and same as use stringify to make it stringify.