Forum Discussion

RK3's avatar
RK3
Occasional Contributor
2 years ago

how to read and validate json file using testcomplete

I have a JSON file want to read and validate that file can you one show me with simple code?

  • Use JSON, here's an example,

     

    function Test1()
    {
        let text = '{ "employees" : [' +
            '{ "firstName":"John" , "lastName":"Doe" },' +
            '{ "firstName":"Anna" , "lastName":"Smith" },' +
            '{ "firstName":"Peter" , "lastName":"Jones" } ]}';
        
        var obj = JSON.parse(text);
        for (var i = 0; i < obj.employees.length; i++) {
            Log.Message(obj.employees[i].firstName);
        }
    }

     

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Use JSON, here's an example,

     

    function Test1()
    {
        let text = '{ "employees" : [' +
            '{ "firstName":"John" , "lastName":"Doe" },' +
            '{ "firstName":"Anna" , "lastName":"Smith" },' +
            '{ "firstName":"Peter" , "lastName":"Jones" } ]}';
        
        var obj = JSON.parse(text);
        for (var i = 0; i < obj.employees.length; i++) {
            Log.Message(obj.employees[i].firstName);
        }
    }

     

     

  • RK3's avatar
    RK3
    Occasional Contributor

    Thanks for the reply if i have two json files and want to validate that files having same content how can we do that?

     

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      files having same content

      Please provide your definition of 'same content'.

      Does it mean 'binary equal'? Do whitespaces/new lines matter? Does order of nodes matter? Any other criteria?

       

      • RK3's avatar
        RK3
        Occasional Contributor

        yes Binary equal . I want to compare two files with same binary value which method should i use to check that?

  • RK3's avatar
    RK3
    Occasional Contributor

    Hi i have two json files with me and i want to compare specific value in the files.

     

    example:

    file1

    {"employess":[{"ID":"1234","Details":[{"Name":"Ravi","Age":"30","Rank":"11"}]},

                         [{"ID":"1235","Details":[{"Name":"Kiran","Age":"35","Rank":"1"}]},

                         [{"ID":"1233","Details":[{"Name":"Rani","Age":"25","Rank":"21"}]}

    file2

    {"employess":[{"ID":"1234","Details":[{"Name":"Ravi","Age":"30","Rank":"11"}]},

                         [{"ID":"1235","Details":[{"Name":"Krisha","Age":"35","Rank":"1"}]},

                         [{"ID":"1233","Details":[{"Name":"Rani","Age":"25","Rank":"21"}]}

     

    i want to validate the data using javascript in testcomplete  like both files having same ID :1235 but the "Name" is different any solution please?