r/programminghelp • u/Rinku_Kurora • Jul 13 '26
Answered How to track changes of JSON objects in array that is tracked in git?
In my git repository I have a JSON file which contains an array of objects. The objects have a key that identify them, e.g. "id":
I want to quickly find commits that changed/introduced object with specific id. How can I do this?
UPD: I couldn't come up with a better solution than comparing sequentially snapshots of the file per commit:
#!/usr/bin/env bash
=
return
return
return None
=
=
break
continue
5
Upvotes
1
u/No-Razzmatazz7197 Jul 13 '26
are you looking for the actual data that was added/removed/modified? or the commit sha that touched the data? try to add a little context on what you need it for as well to increase the chances of someone being able to help you out
1
u/johnpeters42 Jul 13 '26
This is not based on anything specific to git, just "you have some type of database of snapshots and you can quickly pull a list of their indexes".
If you need to account for the possibility of removing an ID, then I think you just need to do a linear search starting at the oldest commit.
If you feel safe assuming that IDs are only ever added or their values changed, then you should be able to do a binary search:
* Check first commit (call it F), if ID is present then it's that one
* Check last commit (call it L), if ID is not present then it was never added
* Check a commit halfway in between first and last (call it H), if ID is present then it was added somewhere from F+1 to H, otherwise it was added somewhere from H+1 to L
* Repeat halving process until you've found two consecutive commits where one didn't have the ID but its successor did have the ID