title: how to update json value in fs which is read from fs in javascript?
body: <p>Requirement: read from json file, update value and write to json file</p>
  <p>I have a json file test.json which contains below data</p>
  <p><strong>test.json</strong></p>
  <pre><code>{
      &quot;login&quot;: &quot;readfromjson&quot;,
      &quot;pwd&quot;: &quot;pwd&quot;,
      &quot;taskName&quot;: &quot;task&quot;,
     
      &quot;selectors&quot;: [
          {
              &quot;Edit&quot;: &quot;Edit&quot;,
              &quot;Complete&quot;: &quot;Complete&quot;,
              &quot;Review&quot;: &quot;Review&quot;
          }
      ]
  }
  </code></pre>
  <p><strong>function.js</strong> //contains function to read, update json value from fs</p>
  <pre><code>function writeToJsonKey(fileName, keyName, value){
      let fullpath = PATH.resolve(GETPATH, fileName);
      let rawdata = FS.readFileSync(fullpath,'utf8');
      console.log(rawdata) //returns data as per json and is of string type
      
      let data = JSON.parse(rawdata); 
      console.log(rawdata) //returns [object Object]
      
      var i;
      keyName = keyName.split('.');
      for (i = 0; i &lt; keyName.length; i++)
      {           
          data = data[keyName[i]];            
      }   
      data[keyName[i]] = value;
  
      console.log(keyName[i]) //returns required key where update to be done
      console.log(data[keyName[i]]) //returns undefined??
      
      FS.writeFileSync(fullpath, JSON.stringify(data, null, 2), function (err) { 
          if (err) throw err;
          console.log('Saved!');
        });
  }
  </code></pre>
  <p><strong>function call</strong></p>
  <pre><code>writeToJsonKey('data.json', 'selectors.0.Edit', 'updated value') 
  </code></pre>
  <p><strong>Result:</strong> <em><strong>As shown in console.log, whole data of json gets replaced with 'Edit'.</strong></em></p>
  <p><strong>Note: If I read json data like below, it works but has issue when read from fs.</strong></p>
  <pre><code>var obj = {
          &quot;login&quot;: &quot;readfromjson&quot;,
          &quot;pwd&quot;: &quot;pwd&quot;,
          &quot;taskName&quot;: &quot;task&quot;,
         
          &quot;selectors&quot;: [
              {
                  &quot;Edit&quot;: &quot;Edit&quot;,
                  &quot;Complete&quot;: &quot;Complete&quot;,
                  &quot;Review&quot;: &quot;Review&quot;
              }
          ]
      }
  </code></pre>