Forum Discussion

Sabiha's avatar
Sabiha
Occasional Contributor
4 years ago

Reading/setting values in appsettings.json file

Hi everyone, 

 

I need to read the localization settings from my appsettings.json  file  on my server and possible update them too as part of my automated testing. Basically, I need to be able check what supported and fallback languages are defined

 

How do I read from  and Update json files in Testcomplete?

 

The format of the section I require from the appsettings file is 

"Localization": {
"SupportedLanguages": [ "en-US", "fr", "zh" ],
"FallbackLanguage": "en-US",
"SupportedResourceFiles": [
"Base",
"ErrorCodes",
"CoreForms",
"Languages",
"ProductTerms",
"MobileClient",
"OtherClient",
"CustomerTerms"
]

}

 

I am using Javascript for my TestComplete scripting language

 

Thanks

  • Sabiha's avatar
    Sabiha
    4 years ago

    Thanks for your reply AlexKaras 

     

    Just wanted to update to say that I had managed to get something working, similar to your code above, but using JSON.parse

     

    var fileContents = aqFile.ReadWholeTextFile(filePath, aqFile.ctANSI)

    var  appSettings = JSON.parse(fileContents)
    var defaultLanguage = appSettings.MyID.Localization.FallbackLanguage
    return defaultLanguage

     

    I haven't tried updating the file yet, haven't need to so far...

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    give me an example of how I would load the file in TestComplete and parse the contents

    Untested sample which, hopefully, will inspire you:

    var s = aqFile.ReadWholeTextFile("appsettings.json", aqFile.ctANSI); // s contains whole file as a string
    
    var oJSON;
    eval('oJSON = {' + s + '};'); // convert string to JSON object (note: string s is NOT enclosed in curly braces and thus they are added
    
    var strFallbackLanguage = oJSON.Localization.FallbackLanguage;
    Log.Message(aqString.Format('FallbackLanguage is "%s"', strFallbackLanguage), strFallbackLanguage);
    
    // Modify some value
    oJSON.Localization.FallbackLanguage = "fr";
    // store changes to the file
    aqFile.WriteToTextFile("appsettings.json", JSON.stringify(oJSON), aqFile.ctANSI, true);

     

    • Sabiha's avatar
      Sabiha
      Occasional Contributor

      Thanks for your reply AlexKaras 

       

      Just wanted to update to say that I had managed to get something working, similar to your code above, but using JSON.parse

       

      var fileContents = aqFile.ReadWholeTextFile(filePath, aqFile.ctANSI)

      var  appSettings = JSON.parse(fileContents)
      var defaultLanguage = appSettings.MyID.Localization.FallbackLanguage
      return defaultLanguage

       

      I haven't tried updating the file yet, haven't need to so far...

    • Sabiha's avatar
      Sabiha
      Occasional Contributor

      Thanks for the reply Marsha_R 

       

      I'm fairly new to Javascript, JSON etc. Could you possibly give me an example of how I would load the file in TestComplete and parse the contents to say for example find the fallbackLanguage from my localization block?