0

How to update a json file array values(particular values) using nodejs.

json file:

{
  "EmailSubjects": {
    "RegistrationMailSubject": "New User Registration",
    "ApprovedMailSubject": "new user Approval ",
    "RejectedMailSubject": "new user Your  ",
    "ForgetPasswordSubject": "password recovery"

  }
}

in this emailsubjects I want to update only registrationmailsubject value only. how to achieve this..

N15
  • 305
  • 2
  • 4
  • 17
  • 1
    The question is not clear: your "json file" is a str variabile, something that you are reading from txt file, a json object? – S-Wing Feb 16 '18 at 10:17

2 Answers2

0
let data = require('./path/to/file.json');
data.EmailSubjects.RegistrationMailSubject = 'New Text';

// if you want to persist the data
let fs = require('fs');
fs.writeFileSync('./path/to/file.json', JSON.stringify(data));
explorer
  • 944
  • 8
  • 18
-1
EmailSubjects.RegistrationMailSubject = "my new value"
sharkdawg
  • 958
  • 1
  • 8
  • 20
  • Do you need to save the updated JSON object back to the .json file? – Miguel Calderón Feb 16 '18 at 10:18
  • If your question is about manipulating the json file content it was answer here https://stackoverflow.com/questions/10685998/how-to-update-a-value-in-a-json-file-and-save-it-through-node-js – CMartins Feb 16 '18 at 10:19
  • This is also server side code in node. Also others have stated, please add more information to your question then we can help. – sharkdawg Feb 16 '18 at 11:00