在将unmarshal用作通用接口时如何验证JSON?

I want to validate byte array data if it contains valid JSON using unmarsall method into interface.

package main

import (
    "encoding/json"
    "fmt"
)

func isJSON(s string) bool {
    var js map[string]interface{}

    return json.Unmarshal([]byte(s), &js) == nil
}

func main() {
    var tests = []string{
        `{"a":"b"}`,
        `[{"a":"b"},{"a":"b"}]`,
    }

    for _, t := range tests {
        fmt.Printf("isJSON(%s) = %v

", t, isJSON(t))
    }

}

Both input test parameters are valid JSON strings, but it validate based on the interface 'map[string]interface{}'

{
    "a": "b"
}

[{
    "a": "b"
}, {
    "a": "b"
}]

I want to validate the JSON text. JSON text is a serialized object or array. Hence looking for a solution which support all valid cases for JSON text as I added test cases in playground.

How can I make this interface i.e var map[string]interface{} generic so it support both test cases of valid JSON string?

Solved: Playground Link

Don't use map[string]interface{} but simply interface{}. The second example is of type []interface{} and there are more types of valid json.

Here your working code. I added a few more cases of valid json.

playground

Here the code if you want to allow only maps and slices:

playground