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
Community Hero

Re: Comparing JSON variable to an Array of objects in JScript

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
[Community Expert Group]
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.


I’ll be talking about “Hidden Treasures – TestComplete Script Extensions” at #SBConnect in October! Who’s joining me? Click here for tickets
Extensions available
Frequent Contributor

Re: Comparing JSON variable to an Array of objects in JScript

We are not converting now. This is a task in summer 2019 Smiley Happy so kinda I would like the JScript solution for now.
Share Your Stories
Read Latest News
Top Kudoed Authors