Ask a Question

Reading/setting values in appsettings.json file

SOLVED
Sabiha
Occasional Contributor

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

5 REPLIES 5
Marsha_R
Champion Level 2

It looks like you can use javascript objects to access your json file contents

 

https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript.html

 

 

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?

 

 

 

 

AlexKaras
Champion Level 2

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);

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
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...

AlexKaras
Champion Level 2

Hi,

 

using JSON.parse

JSON.parse() is acceptable alternative.

Note that it will throw an exception if the parameter is not json-compatible.

https://stackoverflow.com/questions/9804777/how-to-test-if-a-string-is-json-or-not

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
cancel
Showing results for 
Search instead for 
Did you mean: