cancel
Showing results for 
Search instead for 
Did you mean: 

Comparing JSON variable to an Array of objects in JScript

Frequent Contributor

Comparing JSON variable to an Array of objects in JScript

I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.

I got a JSON object with expected values (could have 1 value , 2 or more) I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB. Ex:

My JSON var is :

 var expValues = {
            "id": "123",
            "age": 23
        };

My DB will push some values to an Array of objects.

Ex:

    if ((rs.BOF) && (rs.EOF))
    {
        //nothing found;
    }
    else
    {
        while (!rs.EOF)
        {
            aDetails.push(
            {
                "id": rs.fields("id").Value,
                "name": rs.fields("name").Value,
                "age": rs.fields("age").Value,
                "sex": rs.fields("sex").Value,
                "hobby": rs.fields("hobby").Value
            });
            rs.MoveNext();
        }
    }
     rs.close;
 //Close connection then return
 return aDetails;

This is doable easy using JavaScript but how could I do it with Jscript in testComplete ?

 

let aDetails = [{
  "id": "123",
   "name": "as",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}, {
  "id": "1234",
   "name": "as1",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}, {
  "id": "12",
   "name": "as2",
   "age": 23,
   "sex": "m",
   "hobby": "abc"
}]

var expValues = {
            "id": "123",
            "age": 23
        };
        
function isObjectMatched(obj) {
  return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}

console.log(isObjectMatched(expValues))

 

2 REPLIES 2
Highlighted
Community Hero

If you're using TestComplete 12... there's no need to pick between JScript and JavaScript.  If you have a project that is in JScript, you can convert easily.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Frequent Contributor

We are not converting now. This is a task in summer 2019 🙂 so kinda I would like the JScript solution for now.
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

See replies!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors