Golang supports an API that handles JSON using encoding/json package. we use unmarshal function for decoding a JSON string.
package main import ( "fmt" "encoding/json" ) func main() { fmt.Println("---- json to struct ------") type User struct { UserId intjson:"userId"
Id intjson:"id"
Title stringjson:"title"
Completed booljson:"completed"
} var user User str :={"userId":1,"id":1,"title":"delectus aut autem","completed":false}
err := json.Unmarshal([]byte(str), & user) if err != nil { fmt.Println(err) } fmt.Println("userId :- ",user.UserId) fmt.Println("Id :- ",user.Id) fmt.Println("Title :- ",user.Title) fmt.Println("Completed :- ",user.Completed) }
Output
Conclusion
Finally, we parsed the json in go; stay tuned for more go tutorials.