I have the following JSON config file. I would like to read the JSON file and store into Dictionary object using Java-script.
{
"DefaultTime": 10,
"ExpetTime": 30,
"Environment": "QA",
"DefaultBrowser": "CHROME"
"_Envi": [
{
"PP": [
{
"Server1": "https://login.com",
"UserName": "user1",
"Password": "pass1",
"UserFullName": "userName1",
"UserEmail": "[email protected]",
"User2Name": "user1",
"User2FullName": "userName2",
"User2Password": "pass1",
"User3FullName": "user3",
"User3Name": "userName3",
"User4Name": "user4",
"User4FullName": "userName4",
"User4Email": "[email protected];"
}
]
},
{
"QA": [
{
"Server": "https://gmail.com",
"UserName": "user1",
"Password": "pass1",
"UserFullName": "userName1",
"UserEmail": "[email protected]",
"User2Name": "user2",
"User2Password": "pass1",
"User2FullName": "userName2",
"User3Name": "user3",
"User3FullName": "userName3",
"User4Name": "user4",
"User5Name": "userName4",
"Password4": "pass2",
"User4FullName": "user4",
"User5FullName": "user5"
}
]
},
{
"PROD": [
{
"Server": "https://pro.login.com",
"UserName": "user1",
"Password": "pass1",
"UserFullName": "userName1",
"UserEmail": "[email protected]",
"User2Name": "user2",
"User2FullName": "userName2",
"User2Password": "pass1",
"User3Name": "user3",
"User3FullName": "Username3",
"User4Name": "user4",
"User4FullName": "UserName4",
"User4Email": "[email protected]"
}
]
}
]
}
In the above JSON, I need few environment specific values based on user selection.
If "Environment": "PP", it should need to store following values in to dictionary object.
{
"_Envi": [
{
"PP": [
{
"Server1": "https://login.com",
"UserName": "user1",
"Password": "pass1",
"UserFullName": "userName1",
"UserEmail": "[email protected]",
"User2Name": "user1",
"User2FullName": "userName2",
"User2Password": "pass1",
"User3FullName": "user3",
"User3Name": "userName3",
"User4Name": "user4",
"User4FullName": "userName4",
"User4Email": "[email protected];"
}
]
}
]
}
3
Answers
You can achieve this in JavaScript by reading the JSON file, parsing it, and storing the environment-specific values in a dictionary object:
You can use this code snippet to get your required data:
pass your selection item in getSelectedElement(”)
I hope this will work out