Forum Discussion

prasath_2k's avatar
Occasional Contributor
3 years ago

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.



8 Replies

  • AlexKaras's avatar
    Champion Level 3



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


    • prasath_2k's avatar
      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.



    • mkharsha's avatar
      Occasional Contributor

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

      • chriscc's avatar

        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.WriteValue_33("911 GT3");
              jtw.WriteValue_33("6 Speed");
              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.




  • ishu's avatar
    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.




    • prasath_2k's avatar
      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.



  • rraghvani's avatar
    Champion Level 3

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

  • 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 (

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


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