Golang ProgramsGolang Programs

How to check string is in json format?

package main

import (
    "encoding/json"
    "fmt"
)

func JSONString(str string) bool {
    var jsonStr string
    err := json.Unmarshal([]byte(str), &jsonStr)
    return err == nil
}

func JSON(str string) bool {
    var jsonStr map[string]interface{}
	err := json.Unmarshal([]byte(str), &jsonStr)
    return  err == nil

}

func main() {    
	fmt.Println(JSONString(`"Australia"`))	// true
    fmt.Println(JSONString(`Australia`))	// false
	
	fmt.Println(JSON(`Australia`))	// false
	fmt.Println(JSON(`{"Country":"Australia"}`)) // true
}
C:\golang\codes>go run example.go
true
false
false
true

C:\golang\codes>