A custom JSON diff implementation that keeps array changes intact.
diff_calculator.go
- Calculates differences between two JSON objectsdiff_reconstructor.go
- Reconstructs original JSON from current JSON + diffdemo.go
- Demonstrates both operations
Run all three files together:
go run diff_calculator.go diff_reconstructor.go demo.go
differ := NewJSONDiffer()
diffs, err := differ.Compare(json1, json2)
reconstructor := NewJSONReconstructor()
original, err := reconstructor.ReverseDiff(currentJSON, diffs)
{
"field_name": "arr",
"change_type": "modified",
"from_value": "[1,2,3]",
"to_value": "[1,3,4]",
"full_path": "/fruits/arr",
"value_type": "array"
}
added
- New fieldremoved
- Deleted fieldmodified
- Changed field
Arrays are treated as complete units. Changes like [1,2,3]
→ [1,3,4]
create a single modification entry, not element-by-element diffs.
Uses only Go standard library:
encoding/json
fmt
reflect
sort
strconv
strings