How to Compare json Results using js and deep diff - Gnorion/BizVR GitHub Wiki

const fs = require('fs');
const diff = require('deep-diff');

// Function to read JSON file
const readJsonFile = (filePath) => { return JSON.parse(fs.readFileSync(filePath, 'utf8'));};

// Function to list differences between two JSON objects
const listDifferences = (obj1, obj2) => {
  const differences = diff(obj1, obj2);
  if (differences) {
    differences.forEach((change) => {
      console.log(`Change type: ${change.kind}`); console.log(`Path: ${change.path.join(' -> ')}`);
      if (change.lhs !== undefined) console.log(`Old value: ${change.lhs}`);
      if (change.rhs !== undefined) console.log(`New value: ${change.rhs}`);
      console.log('---');
    });
  } else {console.log('No differences found.');}
};

// Main function to compare two JSON files
const compareJsonFiles = (file1, file2) => {
  try {
    const json1 = readJsonFile(file1);    const json2 = readJsonFile(file2);
    console.log(`Comparing ${file1} with ${file2}`);
    listDifferences(json1, json2);
  } catch (error) {console.error('Error reading or parsing files:', error);}
};

// Replace 'file1.json' and 'file2.json' with your actual file paths
const file1 = 'EMP01 No Tables No Input No Output.jsonv2.01.vv';
const file2 = 'EMP01 No Tables No Input No Output.jsonv2.02.vv';

compareJsonFiles(file1, file2);

``