Detecting JSON changes

92 views
Skip to first unread message

Slawomir Pryczek

unread,
Oct 10, 2022, 10:50:20 AM10/10/22
to golang-nuts
Hi Guys,
I have 2 json files, A, B. Now i want to detect changes on the root level.

File A: {"a":{"b":1, "c":2}, "x":false,  ... }
File B: {"a":{"b":1, "c":2}, "x":true,  ... }

I want to be able to see that x is different between A and B and a stayed the same. What would be the easiest approach?

Thanks

burak serdar

unread,
Oct 10, 2022, 11:01:41 AM10/10/22
to Slawomir Pryczek, golang-nuts
This is not easy in general. Try using an available package, like: github.com/wI2L/jsondiff
 

Thanks

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7e88ac99-b6ff-49af-95a9-c1c5d7cf9236n%40googlegroups.com.

Shulhan

unread,
Oct 10, 2022, 11:16:52 AM10/10/22
to Slawomir Pryczek, golang-nuts
I can think three options here.

One, create a type T that unmarshal the JSON into T and add method that
compare each fields.

Two, create a type T and unmarshal the JSON into T and use third party
library that do struct comparison.

Three, diff the text as is using third party library.

For option two and three, I have some example here:

https://go.dev/play/p/bknJBK4m4CW

-- ms
Reply all
Reply to author
Forward
0 new messages