zt2g6q
Last Updated: September 25, 2018
·
520
· lukascivil

diff json/structures in Javascript

With JsonDiffer you can differentiate two structures in Javascript/typescript:
https://github.com/lukascivil/jsondiffer
live example: http://jsondifference.lukascivil.com.br/

import {JsonDiffer} from 'json-difference';

const coffee = { color: { color1: 'black', color2: 'brown' }, special: true };
const oil = { color: { color1: 'red', color2: 'blue' }, special2: false };

const jsondifference = new JsonDiffer();
let diff = jsondifference.getDiff(coffee, oil);

console.log(JSON.stringify(diff))

Output:
{ "new": { "special2": false }, "removed": { "special": true }, "edited": [{ "color/color1": { "newvalue": "black", "oldvalue": "red" } }, { "color/color2": { "newvalue": "brown", "oldvalue": "blue" } }] }