DEV Community

Nino Filiu
Nino Filiu

Posted on

Cool json git diff command

git diff is great but kinda sucks when reporting about deep keys updates in JSON files, because it only shows the last key, and not the whole object path to the updated key

$ git diff HEAD~1 -- file.json
...
-       "name": "John Doe"
+       "firstName": "John",
+       "lastName": "Doe"
...
Enter fullscreen mode Exit fullscreen mode

But by combining gron with git show $rev:$path, we can have a very pretty output that tells which keys have been updated, and it's almost valid JS!

$ diff <(git show HEAD~1:file.json | gron) <(gron file.json)
< json.users[0].owner.name = "John Doe";
---
> json.users[0].owner.firstName = "John";
> json.users[0].owner.lastName = "Doe";
Enter fullscreen mode Exit fullscreen mode

I found that too elegant not to share. Have fun scripting!

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

That's clever.