Ask a Question

How to read a JSON file

SOLVED
prasath_2k
Occasional Contributor

How to read a JSON file

Hi experts,

 

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.

 

TIA

8 REPLIES 8
AlexKaras
Community Hero

Hi,

 

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

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] 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 Heroes]
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 Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
prasath_2k
Occasional Contributor

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.

 

Thanks

ishu
Occasional Visitor

Hi prasath_2k,

 

I also trying to do the same and searching for the solution. If you have any solution please let me know.

 

Thanks,

IshuS

rraghvani
Trusted Contributor

You could try and use PowerShell to read and format the JSON data and then use TestComplete to perform the various tests you need.

chriscc
Contributor

Hi @prasath_2k 

 

Since the JSON file is a text file you might want to compare files doing one of the following:

1. Use the Files.Compare method: Files.Compare Method | TestComplete Documentation (smartbear.com)

2. Do a simple line-by-line comparison of the contents. Reading each file using the aqFile.OpenTextFile method: aqFile.OpenTextFile Method | TestComplete Documentation (smartbear.com)

 

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

 

JSON.jpg

Chris
prasath_2k
Occasional Contributor

Hi ishu,

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.

 

Hope you found this useful.

 

Thanks

mkharsha
Occasional Contributor

How to dotNet bridge in testcomplete. For example, if I want to use NewtonSoft ?

You browse to the location of your desired dll(s) and load it(them) in. 

chriscc_0-1678483147227.png

 

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.

chriscc_1-1678483147232.png

 

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

 

 

Output file contents:

{"Make":"Porsche","Model":"911 GT3","Engine":["4.0L"/*(640bhp)*/],"Transmission":["6 Speed","Manual"]}

 

I hope this helps.

 

Regards,

 

Chris
cancel
Showing results for 
Search instead for 
Did you mean: