Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RodrigoMattosoSilveira/87391c6f050ae9d64a65cd37a2f70177 to your computer and use it in GitHub Desktop.
Save RodrigoMattosoSilveira/87391c6f050ae9d64a65cd37a2f70177 to your computer and use it in GitHub Desktop.
GO: Convert a string to JSON
// GO: Convert a string to JSON
// Input: string
// Output:JSON if OK, nil otherwise
//
func stringToJSON (sourceString string) []byte {
var obj map[string]interface{}
err := json.Unmarshal([]byte(sourceString), &obj)
if err != nil {
fmt.Println(err)
return nil
}
jsonStr, err := json.Marshal(obj)
if err != nil {
fmt.Println(err)
return nil
}
return jsonStr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment