title: How to compare the two JSON which has different ROW position and index
body: <p>I have a JSON files which I want to compare the values and ensure both of the JSON is having the same values and return TRUE if there is any one of the value is not available then I need to return immediately FALSE.</p>
  <p>The problem is now I'm having the two JSON files with Same Values but the row position is different.</p>
  <p>JSON_1:</p>
  <pre><code>{
      &quot;sqlQuery&quot;: &quot;select type,subtype from wetrade_p2 where parent_id='69341269'&quot;,
      &quot;message&quot;: &quot;2 rows selected&quot;,
      &quot;row&quot;: [
          {
              &quot;column&quot;: [
                  {
                      &quot;value&quot;: &quot;W&quot;,
                      &quot;name&quot;: &quot;TYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;P&quot;,
                      &quot;name&quot;: &quot;STATUS&quot;
                  },
                  {
                      &quot;value&quot;: &quot;0&quot;,
                      &quot;name&quot;: &quot;SUBTYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;USD&quot;,
                      &quot;name&quot;: &quot;CURRENCY&quot;
                  }
              ]
          },
          {
              &quot;column&quot;: [
                  {
                      &quot;value&quot;: &quot;W&quot;,
                      &quot;name&quot;: &quot;TYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;S&quot;,
                      &quot;name&quot;: &quot;STATUS&quot;
                  },
                  {
                      &quot;value&quot;: &quot;0&quot;,
                      &quot;name&quot;: &quot;SUBTYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;USD&quot;,
                      &quot;name&quot;: &quot;CURRENCY&quot;
                  }
              ]
    
          }
      ]
  } 
  </code></pre>
  <p>JSON_2:</p>
  <pre><code>{
      &quot;sqlQuery&quot;: &quot;select type,subtype from wetrade_p2 where parent_id='69341269'&quot;,
      &quot;message&quot;: &quot;2 rows selected&quot;,
      &quot;row&quot;: [
       {
              &quot;column&quot;: [
                  {
                      &quot;value&quot;: &quot;W&quot;,
                      &quot;name&quot;: &quot;TYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;S&quot;,
                      &quot;name&quot;: &quot;STATUS&quot;
                  },
                  {
                      &quot;value&quot;: &quot;0&quot;,
                      &quot;name&quot;: &quot;SUBTYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;USD&quot;,
                      &quot;name&quot;: &quot;CURRENCY&quot;
                  }
              ]
    
          },
          {
              &quot;column&quot;: [
                  {
                      &quot;value&quot;: &quot;W&quot;,
                      &quot;name&quot;: &quot;TYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;P&quot;,
                      &quot;name&quot;: &quot;STATUS&quot;
                  },
                  {
                      &quot;value&quot;: &quot;0&quot;,
                      &quot;name&quot;: &quot;SUBTYPE&quot;
                  },
                  {
                      &quot;value&quot;: &quot;USD&quot;,
                      &quot;name&quot;: &quot;CURRENCY&quot;
                  }
              ]
          }
         
      ]
  }
  </code></pre>
  <p>If you check the above JSON_1 and JSON_2 and both are having same values and but the ROW is mismatched/ JSON objects order is not remain same.Because of this the below code is return FALSE even thought the values are same:</p>
  <p><strong>Code:</strong></p>
  <pre><code>compareActualAndExpectedResponse:function (jsonActual,jsonExpected)  {
  
                  var jsonObject1 = JSON.parse(jsonActual);
                  var jsonObject2 = JSON.parse(jsonExpected);
                  var keys = Object.keys(jsonObject1);
                        for (var i = 0; i &lt; keys.length; i++) {
                                   var key = keys[i];
                                        if (jsonObject1[key] != jsonObject2[key]) {
                                                console.log(key + &quot; value changed from '&quot; + jsonObject1[key] + &quot;' to '&quot; + jsonObject2[key] + &quot;'&quot;);
                                                return false;
                                           }
                          }
                   return true;
  }
  </code></pre>
  <p>UpdatedCode:</p>
  <p><strong>Utils.js:</strong></p>
  <pre><code>var methods = {
  
              getuuid:function () {
                  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                      var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r &amp; 0x3 | 0x8);
                      return v.toString(16);
                    });
               },
  
               compareActualAndExpectedResponse:function (jsonActual, jsonExpected) {
                  if (Object.prototype.toString.call(jsonActual) !==
                    Object.prototype.toString.call(jsonExpected)) {
                        console.log(&quot;false 1&quot;);
                    return false;
                  }
                
                  if (Array.isArray(jsonActual)) {
                    if (jsonActual.length !== jsonExpected.length) {
                        console.log(&quot;false 2&quot;)
                      return false;
                    }
                
                    let i = jsonActual.length;
                
                    while (i--) {
                      let j = jsonExpected.length;
                      let found = false;
                
                      while (!found &amp;&amp; j--) {
                        if (compareActualAndExpectedResponse(jsonActual[i], jsonExpected[j])) found = true;
                      }
                
                      if (!found) {
                          console.log(&quot;false 3&quot;);
                        return false;
                      }
                    }
                console.log(&quot;true 1&quot;)
                    return true;
                  }
                
                  if (Object.prototype.toString.call(jsonActual) === '[object Object]') {
                    for (const key in jsonActual) {
                      if (Object.prototype.hasOwnProperty.call(jsonActual, key)) {
                        if (!Object.prototype.hasOwnProperty.call(jsonExpected, key)) {
                            console.log(&quot;false 4&quot;);
                            return false; 
                        }
                
                        if (!compareActualAndExpectedResponse(jsonActual[key], jsonExpected[key])) {
                          console.log(&quot;false 5&quot;)
                          return false;
                        }
                      }
                    }
                    for (const key in jsonExpected) {
                      if (Object.prototype.hasOwnProperty.call(jsonExpected, key) &amp;&amp;
                        !Object.prototype.hasOwnProperty.call(jsonActual, key)) {
                            console.log(&quot;false 6&quot;)
                        return false;
                      }
                    }
                console.log(&quot;true 2&quot;)
                    return true;
                  }
                
                  return jsonActual === jsonExpected;
                }
          };
  
  module.exports = methods;
  </code></pre>
  <p><strong><strong>Client.JS:</strong></strong></p>
  <pre><code>var utils = require('./Utils');
  
   if(key.includes(&quot;entireJSONResponse&quot;))
                    {
                      var data=fs.readFileSync(global.path+&quot;/validationJSON/JSONResponseValidator/Bank.json&quot;,'utf8')
                      data=data.replace(&quot;{buyer}&quot;,transresult.get(&quot;buyeraccountnumber&quot;));
                      const expected=JSON.stringify(transresult.get(key),null,4);
                      console.log(expected)
                      console.log(data);
                      var validationResult=utils .compareActualAndExpectedResponse(data,expected);
                      console.log(validationResult); //Still return false
                      console.log(utils.compareActualAndExpectedResponse(dataFinal,expected));
                      console.log(&quot;*************************************&quot;)
                    }
  </code></pre>