-
-
Save abhigkar/b0f616c2bec38b9e788f41b89f9ca11a to your computer and use it in GitHub Desktop.
| JSON.parse() - Hands-On [HR] | |
| HR question - Food Items | |
| Solution: | |
| data.json | |
| [ | |
| {"Name":"Pizza", "Type":"Junk Food", "Price":100}, | |
| {"Name":"Salad", "Type":"Good Food", "Price":200}, | |
| {"Name":"Bread", "Type":"Food", "Price":130}, | |
| ] | |
| -------------------------------------------- | |
| JSON Stringify - Hands-On {CP} | |
| ------------------------------------------ | |
| Looping and Creating Object - Hands-On [HR] | |
| Json Data structure: Person object | |
| data.json | |
| { | |
| "persons":{ | |
| "Name":"Abhinav", | |
| "EmployeeID": 12345, | |
| "Experience":1, | |
| "Company":"ABC Inc.", | |
| "Designation":"Systems Analyst" | |
| } | |
| } | |
| ------------------------------------------------ | |
| Verify JSON Datatypes - Hands-On [HR] | |
| Welcome to | |
| JSON schema validation : Change datatypes and return required JSON data | |
| tindex.js | |
| module.exports= function(){ | |
| const data = require("./testdata.json"); | |
| /*Explore the JSON file and return required JSON data*/ | |
| var json = JSON.parse(data).studentData; | |
| json.forEach(function(element, index) { | |
| element['aggregate'] = parseInt(element['aggregate']); | |
| element.forEach(function(per,ind){ | |
| per['sub1'] = parseInt(per['sub1']); | |
| per['sub2'] = parseInt(per['sub2']); | |
| per['sub3'] = parseInt(per['sub3']); | |
| }); | |
| }); | |
| return json; | |
| } | |
| -------------------------------------------------- | |
| JSON Objects - Hands-On {CP} | |
| ----------------------------------------------- | |
| Create JSON - Hands-On {CP} | |
| --------------------------------------- |
loopObject.js
const fs = require('fs');
const foodItems = JSON.parse(fs.readFileSync('./data.json'));
foodItems.forEach((item, index) => {
// Modify the data as per your wish
item.Name += " (Modified)";
item.price += 2.0;
});
console.log(foodItems);
data.json
[
{"Name":"Notes Bureau","Type":"Notes Provider","Price":0},
{"Name":"Notes Bureau","Type":"Solution Provider","Price":0},
{"Name":"Notes Bureau","Type":"Content Provider","Price":0}
]
const data = require("./data.json");
const persons = data.persons;
// Modify the data as per your wish
persons.Name = "John Doe";
persons.Experience = 4;
persons.Company = "ABC Corp";
persons.Designation = "Senior Software Engineer";
console.log(data);
[
{"Name":"Notes Bureau","Type":"Notes Provider","Price":0},
{"Name":"Notes Bureau","Type":"Solution Provider","Price":0},
{"Name":"Notes Bureau","Type":"Content Provider","Price":0}
]
module.exports = function() {
const data = require("./testdata.json");
const jsonData = JSON.parse(data).studentData.map((element) => {
const name = element.Name;
const aggregate = parseInt(element.aggregate);
const percentages = element.percentages.map((per) => {
const sub1 = parseInt(per.sub1);
const sub2 = parseInt(per.sub2);
const sub3 = parseInt(per.sub3);
return { sub1, sub2, sub3 };
});
});
return { studentData: jsonData };
};