skip to Main Content

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


  1. You can achieve this in JavaScript by reading the JSON file, parsing it, and storing the environment-specific values in a dictionary object:

    function readJSONFile() {
      const jsonContent = `{
        "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]"
              }
            ]
          }
        ]
      }`;
    
      return JSON.parse(jsonContent);
    }
    
    function getEnvironmentValues(environment) {
      const jsonObject = readJSONFile(); // Replace this with actual reading from file
      const environmentValues = {};
    
      if (jsonObject._Envi) {
        const specificEnv = jsonObject._Envi.find(env => env[environment]);
        if (specificEnv) {
          environmentValues[environment] = specificEnv[environment];
        }
      }
    
      return environmentValues;
    }
    
    // Usage: Pass the environment as an argument to get the specific values
    const selectedEnvironment = "PP";
    const environmentDictionary = getEnvironmentValues(selectedEnvironment);
    console.log(environmentDictionary);
    Login or Signup to reply.
  2. You can use this code snippet to get your required data:

    const filteredData = data._Envi.map((dict) => {
    if(Object.keys(dict)[0] === "PP"){
        return {"_Envi": [
                {
                    "PP": data._Envi[0].PP
                }
            ]
        };
    }
    });
    
    console.log(filteredData[0])
    
    // Here is the Result
    { _Envi: [ { PP: [Array] } ] } // PP array contain the values
    
    Login or Signup to reply.
  3. // Your JSON data
    var jsonData = {
        "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];"
                    }
                ]
            },
            // ... (other elements in '_Envi' array)
        ]
    };
    
    // Function to get the selected element from '_Envi'
    function getSelectedElement(selectionValue) {
        var selectedElement = {};
        jsonData._Envi.forEach(function (enviElement) {
            if (enviElement[selectionValue]) {
                selectedElement[selectionValue] = [enviElement[selectionValue]];
            }
        });
        return selectedElement;
    }
    
    // Example: Pass 'PP' to get the element of 'PP' from '_Envi'
    var selectedResult = getSelectedElement('PP');
    
    // Convert the result to JSON string with indentation
    var resultJsonString = JSON.stringify(selectedResult, null, 4);
    
    // Output the result
    console.log(resultJsonString);

    pass your selection item in getSelectedElement(”)

    I hope this will work out

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search