How to convert string to JSON in Go

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 a string to JSON is a common operation that is required in many programming tasks. If you’re looking to learn how to convert a string to JSON in Golang, you’re in the right place.

Before diving into the code examples, let’s first understand the theory behind converting a string to JSON in Golang.

In Golang, JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is often used to transmit data between a server and a web application, as an alternative to XML.

Converting a string to JSON involves converting the string into a JSON format, which is a collection of key-value pairs in a specific syntax. Golang provides a built-in package called “encoding/json” that can be used to convert a string to JSON.

Here’s an example of how to convert a string to JSON in Golang:

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    str := `{"name":"John", "age":30, "city":"New York"}`
    var obj map[string]interface{}
    err := json.Unmarshal([]byte(str), &obj)
    if err != nil {
        fmt.Println(err)
        return
    }
    jsonStr, err := json.Marshal(obj)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(jsonStr))
}

In the code above, we declare a string variable called “str” with a value of {"name":"John", "age":30, "city":"New York"}. We then declare a map variable called “obj” with a type of map[string]interface{}.

We use the json.Unmarshal() function to unmarshal the “str” variable and convert it into the “obj” variable. We then use the json.Marshal() function to marshal the “obj” variable into a JSON string, which we store in a variable called “jsonStr”. We then print the “jsonStr” variable using the fmt.Println() function.

So why would someone want to convert a string to JSON in Golang? One common use case is when working with external APIs that return data in the form of a string. By converting the string to JSON, you can easily work with the data and manipulate it in a more structured way.

Another use case is when working with data storage systems that require data to be in a specific format. By converting the data to JSON, you can easily store and retrieve the data in a structured way.

In conclusion, converting a string to JSON in Golang is a common operation that you’ll encounter frequently in your Golang programming journey. By using the json.Unmarshal() and json.Marshal() functions, you can easily convert a string to JSON and manipulate the data in a more structured way. This can help you work with external APIs and data storage systems more efficiently.

I hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them below.


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!