I have searched my many online articles of parsing json array or there exists any npm package to do it.But my all efforts gone in vain.
I have an json array like this =>
{
"pctProjects": [
{
"ID": "1",
"Name": "Software Upgrade",
"Desc": "GO! V1 Chapter 5- EOC Mastery Exercise",
"AppId": "1",
"UserId": "1",
"CreatedDate": "2008-07-30T00:00:00",
"Score": "100",
"SeriesID": "2",
"IsPublished": "1",
"PublishedLMSVariationID": "5",
"IsPCTActive": "0",
"IVTEnabled": "0",
"IsActiveInSelectPopup": "1",
"Chapter": "CH05"
},
{
"ID": "2",
"Name": "Business Venture",
"Desc": "Exploring Volume 1 Chapter 3- TST Exercise",
"AppId": "1",
"UserId": "1",
"CreatedDate": "2008-07-30T00:00:00",
"Score": "100",
"SeriesID": "1",
"IsPublished": "1",
"PublishedLMSVariationID": "7",
"IsPCTActive": "0",
"IVTEnabled": "0",
"IsActiveInSelectPopup": "1",
"Chapter": "CH03"
}
.
.
.
I looped through my json array "pctProjects" using Json.parse method and able to find the object using the property PublishedLMSVariationID which i need to replace with another value, i'm using filesystem module functions like appendFileSync() and writeFileSync() to update the file.But using this methods i have to rewrite other objects data also which i'm not changing and this is not optimised method to do this as i can have n no of objects in that array.
And Using replace-in-file also not helping me to achieve my goal.
Also adding my code snippet what i'm doing right now which is not optimised.
for(let item of gulpJson.pctProjects){
// console.log(typeof touseVariationId)
if(counter==1 && item.PublishedLMSVariationID == results[0]){
item.PublishedLMSVariationID = results[1]
fs.writeFileSync(path.join(dir,'PCT5_MasterPCTProjectsForGulp.json'), JSON.stringify(item,null,4));
counter++;
}
else if(counter==1 && item.PublishedLMSVariationID != results[0]){
fs.writeFileSync(path.join(dir,'PCT5_MasterPCTProjectsForGulp.json'), JSON.stringify(item,null,4));
counter++;
}
else if(item.PublishedLMSVariationID == results[0]){
item.PublishedLMSVariationID = results[1]
fs.appendFileSync(path.join(dir,'PCT5_MasterPCTProjectsForGulp.json'), JSON.stringify(item,null, 4));
// break
// fs.writeFileSync(path.join(dir,'PCT5_MasterPCTProjectsForGulp.json',null, 2), JSON.stringify(item));
}
else
fs.appendFileSync(path.join(dir,'PCT5_MasterPCTProjectsForGulp.json'),","+"\n"+"\t"+ JSON.stringify(item,null, 4));
}
Questions:
- Is there any way to just replace my one json property in json array in nodejs???
Much Obliged:).Thanks in advance.
Please comment if any more information is needed.