If you have a JSON string, you can decode it with Go (golang). In the previous article you learned how to encode to a JSON string. In this article you see how to decode that data again.

The json string used in this article is:

[{"Username":"debora","Password":"123456"},{"Username":"bob","Password":"42"},{"Username":"sandra","Password":"33"}]

Read from json data, then decode it back into the data structure. For simpliciy, read the json data from a file.

Read json data

In the real world, you most likely get a json string from the internet. But for this example thats not necessary.

The program below reads data from stdin, this is keyboard input. You can pass the json as keyboard input, by using the command shown below.

package main

import (
    "bufio" 
    "fmt"
    "os"
)

func main() {
    var input []byte
    for in := bufio.NewScanner(os.Stdin); in.Scan(); {
        input = append(input, in.Bytes()...)
    }

    fmt.Println(string(input))
}

Then run it with the command below:

go run main.go < data.json

If you have a small json string, you can define it as a byte array. In that case, you need to use the escape character (backslash) for every quote.

var jsoninput = []byte("[{\"Username\":\"debora\",\"Password\":\"123456\"},{\"Username\":\"bob\",\"Password\":\"42\"},{\"Username\":\"sandra\",\"Password\":\"33\"}]")

Decode JSON

Declare your data structure again. The data structure needs to match that of the input JSON. In this example, you can use the same data structure that you used for encoding.

type user struct {
    Username string
    Password string
}

Sometimes you need to create the struct yourself, because it may be the json data is coming from an API or app that was made in another programming language.

To decode json in Go (golang), you use the function Unmarhsal().

var users []user
err := json.Unmarshal(input, &users)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(users)

Functions cannot directly modify variables, thats why you need to use a pointer, you can do that by adding the character &. A pointer points to the memory address of the variable, where the value resides in the computers memory.

You can pretty print it like so:

for _, user := range users {
    fmt.Println("->" user.Username)
}

The complete program like this:

package main

import (
    "fmt"
        "encoding/json"
)


type user struct {
    Username string
    Password string
}


func main() {
        var jsoninput = []byte("[{\"Username\":\"debora\",\"Password\":\"123456\"},{\"Username\":\"bob\",\"Password\":\"42\"},{\"Username\":\"sandra\",\"Password\":\"33\"}]")
    var users []user
        err := json.Unmarshal(jsoninput, &users)
        if err != nil {
           fmt.Println(err)
           return
        }
        fmt.Println(users)

        for _, user := range users {
          fmt.Println("->", user.Username) 
        }
}

The output for this program is then:

[{debora 123456} {bob 42} {sandra 33}]
-> debora
-> bob
-> sandra