Skip to content

Instantly share code, notes, and snippets.

@jrockway
Created February 27, 2021 06:57
Show Gist options
  • Save jrockway/73982949b3d2ce9b443528042c4562d4 to your computer and use it in GitHub Desktop.
Save jrockway/73982949b3d2ce9b443528042c4562d4 to your computer and use it in GitHub Desktop.
Diff JSON files
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/google/go-cmp/cmp"
)
func main() {
nameA, nameB := os.Args[1], os.Args[2]
a, b := map[string]interface{}{}, map[string]interface{}{}
bytesA, err := os.ReadFile(nameA)
if err != nil {
log.Fatalf("read %s: %v", nameA, err)
}
bytesB, err := os.ReadFile(nameB)
if err != nil {
log.Fatalf("read %s: %v", nameB, err)
}
if err := json.Unmarshal(bytesA, &a); err != nil {
log.Fatalf("unmarshal %s: %v", nameA, err)
}
if err := json.Unmarshal(bytesB, &b); err != nil {
log.Fatalf("unmarshal %s: %v", nameB, err)
}
diff := cmp.Diff(a, b)
fmt.Println(diff)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment