Answers for "Python compare two JSON files and get difference"

1

python compare two json objects and get difference

# import this package and see the difference
from jsondiff import diff
diff(json1, json2)
Posted by: Guest on August-21-2021
0

search for diff in two JSON

function getDifference(o1, o2) {
    var diff = {};
    var tmp = null;
    if (JSON.stringify(o1) === JSON.stringify(o2)) return;

    for (var k in o1) {
      if (Array.isArray(o1[k]) && Array.isArray(o2[k])) {
        tmp = o1[k].reduce(function (p, c, i) {
          var _t = getDifference(c, o2[k][i]);
          if (_t)
            p.push(_t);
          return p;
        }, []);
        if (Object.keys(tmp).length > 0)
          diff[k] = tmp;
      } else if (typeof (o1[k]) === "object" && typeof (o2[k]) === "object") {
        tmp = getDifference(o1[k], o2[k]);
        if (tmp && Object.keys(tmp) > 0)
          diff[k] = tmp;
      } else if (o1[k] !== o2[k]) {
        diff[k] = o2[k]
      }
    }
    return diff;
  }
Posted by: Guest on March-07-2021

Code answers related to "Python compare two JSON files and get difference"

Python Answers by Framework

Browse Popular Code Answers by Language