Parsing JSON with JavaScript

Can someone please help me step in the right direction? Thanks a lot for any hints.

 var credentials = { steam: {}, rpc: {} };
 var rawCredentials = JSON.parse(fs.readFileSync("auth.json", { "encoding": "utf8" }));
 credentials.steam.accountName = rawCredentials.steam.accountName;
 credentials.steam.password = rawCredentials.steam.password;
 credentials.steam.shaSentryfile = new Buffer(rawCredentials.steam.shaSentryfile, "hex");
 credentials.rpc.username = rawCredentials.rpc.username;
 credentials.rpc.password = rawCredentials.rpc.password;

      

auth.json file

 {
 "credentials.steam.accountName": "XXX",
 "credentials.steam.password": "XXX",
 }

      

Cannot read property "accountName" from undefined

+3


source to share


3 answers


Your property key is "credentials.steam.accountName". You cannot use dot symbols to navigate to credentials or pairs objects because they are not objects. Use to access the values: rawCredentials['credentials.steam.accountName']

.

Edit: if you want to use rawCredentials.credentials.steam.accountName

your JSON should look like this:



rawCredentials = {
  credentials: {
    steam: {accountName: 'foo', ...}
  }
}

      

+6


source


These are fully qualified property names that contain periods, not actual nested objects, in your JSON file.

Also, you forgot a part .credentials

. Use parenthesis notation instead :



credentials.steam.accountName = rawCredentials["credentials.steam.accountName"];
credentials.steam.password = rawCredentials["credentials.steam.password"];

      

(or refactor your JSON)

+3


source


Don't know if this might be the problem, but you have bad syntax in your auth.json:

{
  "credentials.steam.accountName": "XXX",
  "credentials.steam.password": "XXX"
}

      

+1


source







All Articles