How to Convert String to JSON in GoLang

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Converting strings to JSON is an essential part of working with data in Go, and this article will walk you through the process of converting strings to JSON in Go using the built-in encoding/json package.
Go’s standard library provides a comprehensive set of tools for working with data, including support for JSON (JavaScript Object Notation), which is a common format used for representing structured data. The encoding/json package provides a simple way to convert Go values into their equivalent JSON representations and vice versa. In this article, we’ll learn how to use the encoding/json package to convert strings to JSON in Go.
Step 1: Importing the Encoding/JSON Package
To start working with the encoding/json package, you need to import it into your Go code. You can do this by adding the following line of code at the top of your file:
import “encoding/json”
Once you’ve imported the encoding/json package, you can start using its functions to convert strings to JSON.
Step 2: Creating a String Value
To demonstrate how to convert a string to JSON, let’s first create a sample string value in Go. You can do this by declaring a variable and assigning it a string value:
var str = “Hello, World!”
Now that you have a string value, you can use the json.Marshal function to convert it into its JSON representation:
jsonBytes, err := json.Marshal(str)
If the conversion is successful, the jsonBytes variable will contain the JSON representation of the string. If there’s an error during the conversion process, the err variable will contain a non-nil value and you can use it to debug your code.
Step 3: Inspecting the Result
To see what the jsonBytes variable contains after the marshaling process, you can print its contents using the fmt package:
fmt.Println(string(jsonBytes))
This will output the JSON representation of the string value as a string. If you want to inspect the result more closely, you can use a tool like jq or Visual Studio Code’s built-in JSON viewer to pretty print the JSON data.
Step 4: Converting Back to Go Value
Once you have a JSON representation of your data, you might need to convert it back into its original Go value. You can do this using the json.Unmarshal function:
var result string
err := json.Unmarshal(jsonBytes, &result)
If the conversion is successful, the result variable will contain the original string value that was marshaled into JSON. If there’s an error during the unmarshaling process, the err variable will contain a non-nil value and you can use it to debug your code.
Conclusion
In this article, we learned how to convert strings to JSON in Go using the encoding/json package. We created a sample string value, used the json.Marshal function to convert it into its JSON representation, and then inspected the result using the fmt package. Finally, we demonstrated how to use the json.Unmarshal function to convert the JSON back into its original Go value. By following these steps, you can start working with JSON data in your Go applications today!


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!