0

I want to retrieve the values ( username, password, url ) from json file using dynamic variable value in Node js file.

JSON file -> config.json

{
  "ro2" : { 
    "username": "testUser1",
    "password": "test",
    "url": "testUrl"
  },
  "sy2" : { 
    "username": "test1",
    "password": "test1",
    "url": "test1Url"
  }
}

Node js file

A function is defined which has datacentre( ro2, sy2 ) parameter passed value . If i hardcode, i am able to retrieve the exact value by using below var username= config.ro2.username; console.log(username) // Return testUser1 which i needed

but if i use dynamic value

var username= config.${dataCenter}.username; console.log(username) // Return config.ro2.username which is incorrect

function (dataCenter) {
var config = require('./configuration/config.json');

var username= `config.${dataCenter}.username`;
var password= `config.${dataCenter}.password`;
console.log(username) \\ Return config.ro2.username which is incorrect, required testUser1 value
console.log(password) \\ 
}

How i get the value from json by using dynamic value passed dataCenter ( ro2, sh2)??

user2641452
  • 105
  • 2
  • 10

0 Answers0