Scan this QR code to download the app now

Or check it out in the app stores

r/golang icon

Go to golang

how to compare two deeply nested json objects?

help

If all you need is to check whether they equal or not, just do bytes.Equal.

If you are looking for an accurate diff you can use testfy's JSON Equal here.

That function introduces an extraneous *testing.T (for this context), which is hard to fake up in a non-testing context because it wants to fail if the two things are not equal, which is basically a panic.

You can take the source of it and build a version without the t value. Basically use reflect.DeepEqual on the result of unmarshaling into the two anys.

More replies More replies

reflect.DeepEqual on map[string]any instances you unmarshaled your json data into.

If you only care whether they're identical you can just compare the bytes.

Only if you can guarantee the ordering of keys is the same in each though, right?

More replies

Only if you can guarantee the ordering of keys is the same in each though, right?

More replies

You can do this: reflect.DeepEqual(newConfig, globals.Config)

More replies