Hello I have to json string I have to compare them but I don't know how to it
First string
{"timestamp":"2021-10-26T15:09:35.322019337Z","prices":[{"country":"fr","price":{"recommended":0}},{"country":"at","price":{"recommended":0}},{"country":"de","price":{"recommended":0}}]}
Second string
{"timestamp":"2021-10-26T15:09:35.322019337Z","prices":[{"country":"fr","price":{"recommended":0}},{"country":"de","price":{"recommended":0}},{"country":"at","price":{"recommended":0}}]}
as you can see there is a different order in the array.
CodePudding user response:
You can unmarshal them, and use reflect.DeepEqual
:
var v1, v2 interface{}
json.Unmarshal([]byte(s1),&v1)
json.Unmarshal([]byte(s2),&v2)
if reflect.DeepEqual(v1,v2) {
// They are the same
} else {
// They are different
}
There are also other JSON comparison packages out there.
CodePudding user response:
Here's a simple method for comparing strings:
if strings.Compare(string1, string2) != 0 {
return errors.New("strings don't match")
}