Reading/setting values in appsettings.json file
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It looks like you can use javascript objects to access your json file contents
https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript.html
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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);
/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
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
/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
================================
