compare two JSON

by Gerald Gillespie

JavaScript

/*
iterate over the master object.  This is the source of truth for keys

the values Master are the datatypes. Check if the value of the example match those datatypes OR are null. 

recurse into the subkeys

extrakeys shows the entries that are not in the other.  ⚠️ EXAMPLE.extraKeys should be empty!

badValues shows the entries that do not match the datatype.  again EXAMPLE.badValues should be empty


repeate in the reverse direction -- iterating over the EXAMPLE object
*/
console.clear();
(function(EXAMPLE, MASTER) {

  const summary = {};
  summary.EXAMPLE = Object.assign({}, {
    extraKeys: [],
    badValues: [],
    nulledValues : []
  });
  summary.MASTER = Object.assign({}, {
    extraKeys: [],
    badValues: [],
    nulledValues : []
  });

  /**
  CANDIDATE : { object : EXAMPLE , summary : summary.EXAMPLE}
  */
  function analyzeObjects(CANDIDATE, STANDARD, flipped = false) {
    let candidateObj = CANDIDATE.object;
    let standardObj = STANDARD.object;
    let candidateSummary = CANDIDATE.summary;
    let standardSummary = STANDARD.summary;
    //loop through the master object
    for (let [key, dataTypeOrValue] of Object.entries(standardObj)) {
      let candidateValue = candidateObj[key];

      //if both are objects in recurse
      if (typeof dataTypeOrValue === 'object' 
      && typeof candidateValue === 'object' 
      && dataTypeOrValue !== null 
      && candidateValue !== null)
        //recurse
        // console.log('recursion should happen here');
        analyzeObjects({
            object: candidateObj[key],
            summary: candidateSummary
          }, {
            object: standardObj[key],
            summary: standardSummary
          },
          flipped
        );

      else if (( typeof dataTypeOrValue === 'object' && candidateValue === null && !flipped))
        candidateSummary.nulledValues.push([key, typeof dataTypeOrValue ])
        
      
      //if only the datatype-supplying object is an object 
  //    else if...