-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_JSON_Operations.js
More file actions
65 lines (54 loc) · 2.06 KB
/
Copy path4_JSON_Operations.js
File metadata and controls
65 lines (54 loc) · 2.06 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
// Sample JSON String
const jsonString = '{"name": "Lalit", "age": 30, "skills": ["JavaScript", "React", "Node.js"]}';
// 1. Parsing JSON (Convert JSON String to JavaScript Object)
const parsedData = JSON.parse(jsonString);
console.log("Parsed JSON Object:", parsedData);
// 2. Stringifying JSON (Convert JavaScript Object to JSON String)
const jsonObject = { name: "John", age: 25, city: "New York" };
const jsonStringified = JSON.stringify(jsonObject);
console.log("\nStringified JSON:", jsonStringified);
// 3. Accessing JSON Data
console.log("\nAccessing JSON Properties:");
console.log("Name:", parsedData.name);
console.log("Skills:", parsedData.skills[0]); // Output: JavaScript
// 4. Modifying JSON Data
parsedData.age = 31;
parsedData.skills.push("GraphQL");
console.log("\nModified JSON Object:", parsedData);
// 5. Looping Through JSON Data
console.log("\nLooping Through JSON Data:");
for (let key in parsedData) {
console.log(`${key}: ${parsedData[key]}`);
}
// 6. Handling Nested JSON Objects
const nestedJSON = {
person: {
name: "Alice",
details: {
age: 28,
city: "San Francisco"
}
}
};
console.log("\nAccessing Nested JSON:");
console.log("Person's Name:", nestedJSON.person.name);
console.log("Person's City:", nestedJSON.person.details.city);
// 7. Filtering JSON Data (Example: Extracting Skills with 'J')
const filteredSkills = parsedData.skills.filter(skill => skill.startsWith("J"));
console.log("\nFiltered Skills (Starting with 'J'):", filteredSkills);
// 8. Checking if a JSON String is Valid
const isValidJSON = (str) => {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
console.log("\nChecking Valid JSON:");
console.log("Is jsonString valid?", isValidJSON(jsonString)); // true
console.log("Is invalid JSON valid?", isValidJSON("{name: 'test'}")); // false
// 9. Merging Two JSON Objects
const additionalData = { country: "India", city: "Jaipur" };
const mergedData = { ...parsedData, ...additionalData };
console.log("\nMerged JSON Object:", mergedData);