I want to create a function to receive an input string which can be string in json format or just a string. For example, something easy like following function.
Standard encoding/json
library contains json.Valid function starting from go 1.9 - see https://github.com/golang/go/issues/18086 . This function may be used for checking whether the provided string is a valid json:
if json.Valid(input) {
// input contains valid json
}
But json.Valid
may be quite slow comparing to third-party solutions such as fastjson.Validate, which is up to 5x faster than the standard json.Valid
- see json validation
section in benchmarks.