Last active
June 9, 2024 21:52
-
-
Save RodrigoMattosoSilveira/87391c6f050ae9d64a65cd37a2f70177 to your computer and use it in GitHub Desktop.
GO: Convert a string to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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