go get github.com/mrutkows/go-jsondiff
The JSON formatted output from a comparison is based upon a simplified methodology described originally here:
- https://github.com/benjamine/jsondiffpatch/blob/master/docs/deltas.md for the original description.
a value was added, i.e. it was undefined and now has a value.
delta = [ newValue ]
internal representation:
// delta.(type) == *diff.Added
type Added struct {
postDelta // postDelta{position} where `position` is an interface with a String() method
// "PostPosition() interface"
Value interface{} // added value (i.e., 'newValue')
// "Value()" interface
similarityCache
}
- "add" operation has no "preDelta" value, only a "postDelta" value
a value was deleted, i.e. it had a value and is now undefined
delta = [ oldValue, 0, 0 ]
- "delete" operation has no "postDelta" value, only a "preDelta" value
a value was replaced by another value
delta = [ oldValue, newValue ]
value is an array, and there are nested changes inside its items
delta = {
_t: 'a',
index1: innerDelta1,
index2: innerDelta2,
index5: innerDelta5
}
- only indices with "inner" deltas are included
_t: 'a'
: this tag indicates the delta applies to an array,- if a regular object (or a value type) is found when patching, an error will be thrown
internal representation:
delta.(type) == *diff.Array
an item was moved to a different position in the same array
delta = [ '', destinationIndex, 3]
- '': represents the moved item value (suppressed by default)
- 3: indicates "array move"
{
"arr": [
0: "arr0",
1: 21,
2: {
"num": 1,
- "str": "pek3f"
+ "str": "changed"
},
3: [
0: 0,
- 1: "1"
+ 1: "changed"
]
],
"bool": true,
"num_float": 39.39,
"num_int": 13,
"obj": {
"arr": [
0: 17,
1: "str",
2: {
- "str": "eafeb"
+ "str": "changed"
}
],
+ "new": "added",
- "num": 19,
"obj": {
- "num": 14,
+ "num": 9999
- "str": "efj3"
+ "str": "changed"
},
"str": "bcded"
},
"str": "abcde"
}
When you prefer the delta format of jsondiffpatch, add the -f delta
option.
jd -f delta one.json another.json
This command shows:
{
"arr": {
"2": {
"str": [
"pek3f",
"changed"
]
},
"3": {
"1": [
"1",
"changed"
],
"_t": "a"
},
"_t": "a"
},
"obj": {
"arr": {
"2": {
"str": [
"eafeb",
"changed"
]
},
"_t": "a"
},
"new": [
"added"
],
"num": [
19,
0,
0
],
"obj": {
"num": [
14,
9999
],
"str": [
"efj3",
"changed"
]
}
}
}
This package is based upon a fork of https://github.com/yudai/gojsondiff and includes the LCS algorithm implemented in https://github.com/yudai/golcs.
Creation of this derivative package was necessitated as both these package repositories (and their dependencies) have not been maintained for over 6 years with known bugs and vulnerabilities accumulating.
Copies of the licenses from these originating projects are included here: