Max Rheault
Max Rheault

Reputation: 27

How to read data from a json file using node.js

I need to read my user Id to know what the timestart number is but I don't find the way to read the data.

//My code
const fs = require('fs');
var timePath = 'test.json'
var TimeRead = fs.readFileSync(timePath);
var timeFile = JSON.parse(TimeRead); //ready for use
var userId = "2"

var Time = TimeRead.userId.TimeStart; 

console.log(Time)

1 is my user Id variable

//My json File
{
  "1": {
    "TimeStart": 1626909816680
  },
  "2": {
    "TimeStart": 1627166305644
  }
}

Upvotes: 0

Views: 89

Answers (1)

Codebling
Codebling

Reputation: 11382

var Time = TimeRead.userId.TimeStart; 

You can't use a variable with .

Try this:

var Time = TimeRead[userId].TimeStart; 

Upvotes: 1

Related Questions