Created
February 27, 2021 06:57
-
-
Save jrockway/73982949b3d2ce9b443528042c4562d4 to your computer and use it in GitHub Desktop.
Diff JSON files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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