Forum Discussion

prasath_2k's avatar
prasath_2k
Occasional Contributor
2 years ago
Solved

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

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

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

     

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

    • mkharsha's avatar
      mkharsha
      Occasional Contributor

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

      • chriscc's avatar
        chriscc
        Contributor

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

         

         

        Output file contents:

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

         

        I hope this helps.

         

        Regards,

         

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

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

  • rraghvani's avatar
    rraghvani
    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 (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.).