I wanted to read and work with JSON files. Is there best ways to read JSON files using TestComplete? I wanted to trigger the script to fetch the JSON file and do curtains operation by fetching the data from it. Plz suggest best ways to do so.
It depends on the scripting language your test project is based on.
JScript and JavaScript (as well as descendant C#Script and C++Script) support JSON internally.
For VBScript and DelphiScript you will have to use some third-party library (e.g. NewtonSoft for .Net via .Net Bridge in TC if I remember the name right).
I am using JScript language and BDD framework. So I need to read a JSON file and compare the data from it with the another. So how to achieve this? Can you show an example for same it will be helpful.
You browse to the location of your desired dll(s) and load it(them) in.
Then in your code (I use JavaScript) you can call static methods or instantiate an object through the dotNet object. Note: scripting languages like Javascript don’t support method overloading, so for each constructor method you will see zctor(), zctor_1(), zctor_2(), etc. where each will show you the needed parameters.
Below is sample code that writes a JSON file. You might need to add a reference to System.Runtime.dll to access string builder, text writer and file writer.
function NewtonSoft()
{
var sb = dotNET.System_Text.StringBuilder.zctor();
var sw = dotNET.System_IO.StringWriter.zctor_3(sb);
var jtw = dotNET.Newtonsoft_Json.JsonTextWriter.zctor(sw);
jtw.WriteStartObject();
jtw.WritePropertyName("Make");
jtw.WriteValue_33("Porsche");
jtw.WritePropertyName("Model");
jtw.WriteValue_33("911 GT3");
jtw.WritePropertyName("Engine");
jtw.WriteStartArray();
jtw.WriteValue_33("4.0L");
jtw.WriteComment("(640bhp)");
jtw.WriteEnd();
jtw.WritePropertyName("Transmission");
jtw.WriteStartArray();
jtw.WriteValue_33("6 Speed");
jtw.WriteValue_33("Manual");
jtw.WriteEnd();
jtw.WriteEndObject();
dotNET.System_IO.File.WriteAllText("C:\\test.txt", sb.ToString());
}
I am trying to use "eval" function. Initially fetch the Json file from bath and convert the file content to object. Then each element of the file can be used to fetch data from file. Let me attach the code spinet below which might be useful.
//Get the path of JSon file and read the file// path = Project.Path var JSonFilepath = path+"Test Data\\"+"DisplayTab.json"; .....(DisplayTab.Json is my file name)
//Reading the Text file//
var data = aqFile.ReadWholeTextFile(JSonFilepath, aqFile.ctANSI);
//Converting the Text file to a single object// var TestObj = eval("(" + data + ")") ......("TestObj" is entire Text file converted to single object)
Now we have designed our Json file with our desired with nodes and sub nodes, we can easily access the each of it present in Json file using that "TestObj".
Example: TestObj.Node.SubNode --> Will fetch that particular value.
As AlexKaras stated JScript supports JSON, so you could read it into an object, but the comparison of objects can be difficult. Regardless, below is a screenshot, with code, that shows a JSON object created from a GIS Json file. It shows how to access each of the objects properties (i.e., features, geometry, rings, etc.).