0

Want to verify all list from referjson should be present in response json (response).

referjson = [
    {
      a: 1,
      b: 2,
      c: 4,
      tag: "test"
    }, 
    {
      a: 3,
      b: 5,
      tag: "mock"
    }, 
    ...
];

response = [
    {
        tag: "mock",
        a: 3,
        b: 5,
        c: 0,
        d: 0,
        e: 0
    },
    {
        tag: "test",
        a: 1,
        b: 2,
        c: 4,
        d: 0,
        e: 0
    },
    {
        tag: "mocktest",
        a: 3,
        b: 5,
        c: 0,
        d: 0,
        e: 0
    },
    ...
];

Kindly help me to check one by one all the list ie { a: 1, b: 2, c: 4, tag: "test" } should be present in response then check for {a: 3, b: 5, tag: "mock" } and so on ..

function checkJSON(referjson, response) {
    for (var i = 0; i < referjson.length; i++) {
        if (response.contains(referjson[i])) {
            print("**PASS");
        }
        else {
            karate.log(x[i] + "-------------Fail");
        }
    }
}

My if doesn't do it for me.

batman567
  • 826
  • 2
  • 12
  • 23
Rama
  • 815
  • 2
  • 17
  • 37
  • 1
    There is no easy way to do that. [_.isEqual](https://underscorejs.org/#isEqual) from underscore, and [_.eq](https://lodash.com/docs/4.17.15#eq) from lodash could help – lastr2d2 Mar 15 '21 at 11:35
  • 1
    Does this answer your question? [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Justinas Mar 15 '21 at 11:36
  • @Justinas, Thanks here it comparing to json object matched but i want to verify all the contains of json list should present in anther json – Rama Mar 15 '21 at 11:41
  • What have you done to debug your code? Did you tried putting debugger breakpoint before your IF and checking values? – Justinas Mar 15 '21 at 11:44
  • @Justinas, yes , in debugger all are good . only thing is , response.contains ( { a: 1, b: 2, c: 4, tag: "test" }) is not a right way to check if response contains the same json array – Rama Mar 15 '21 at 11:48
  • if you have a method to compare one object to another, then you can replace `.contains` with [Array.prototype.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – lastr2d2 Mar 16 '21 at 06:08

1 Answers1

0

I came with something like this...

const checkJson = (referjson, response) => {
    //will have all the keys of obj so we can compare with response has all the keys too.
    let keyValueIsEqual = [];
    let flag;
    let indexResponse = 0;
    let indexReferjson = 0;
    let numberOfEqualObj = 0;
    while(indexResponse < response.length && indexReferjson < referjson.length){
        const referjsonEntries = Object.entries(referjson[indexReferjson]);
        const responseEntries = Object.entries(response[indexResponse]);
        if(referjsonEntries.length === responseEntries.length) {
            for (const [key, value] of referjsonEntries) {
                flag = false;
                for(const [keyRes, valueRes] of responseEntries) {
                    if(keyRes === key && value === valueRes) {
                        keyValueIsEqual.push(true);
                        flag = true;
                    }
                }
                if(!flag) {
                    indexResponse++;
                    keyValueIsEqual = [];
                    break;
                }
                if(keyValueIsEqual.length === referjsonEntries.length) {
                    //response has all the keys and values [key, value] in one object -> meaning that they have equal objects.
                    indexReferjson++;
                    indexResponse = 0;
                    keyValueIsEqual = [];
                    numberOfEqualObj++;
                }
            }
        } else {
            indexResponse++;
            keyValueIsEqual = [];
        }
    }
    if (numberOfEqualObj === referjson.length) {
        //response have all objs that referjson contain
    } else {
        //response doesn't have all the objs that referjson contain
    }
}

In your example, "referjson" and "response" are not actually jsons, they're an array of objects, so we can iterate over their's entries... I check if "referjsonEntries.length" is equal to "responseEntries.length" because the object will only be equal if they have the same quantity of properties. I created an array "keyValueisEqual" that will store "true" in case the object we're iterating over in "response" has the key and value equals to the object in "referjson". Created a flag that indicates that the object of "reponseEntries" has the key and value equals to the object of "referjsonEntries". So now we only need to check if "keyValueIsEqual" has same length of "referjsonEntries", wich means that response's object is equal to referjson's object, increase the number of "numberOfEqualObj" and in the end of the code I check if "numberOfEqualObj" has the same length of "referjson", wich means that "response" has at least all the objects that "referjson" has. Don't think it's the optimized solution, but it works.

Otávio
  • 77
  • 7
  • 1
    note that this if "if(keyValueIsEqual.length === referjsonEntries.length)" means that the "response" contains "referjsonEntries" that is an object – Otávio Mar 15 '21 at 13:24