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:

1
[{"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.

Related course: Introducing Go: Build Reliable, Scalable Programs

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
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.

1
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.

1
2
3
4
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().

1
2
3
4
5
6
7
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:

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

The complete program like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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:

1
2
3
4
[{debora 123456} {bob 42} {sandra 33}]
-> debora
-> bob
-> sandra