11

Here you can see this code:

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    map1 := map[string]map[string]interface{}{}
    map2 := map[string]interface{}{}
    map2["map2item"] = "map2item"
    map1["map2"] = map2
    fmt.Println(string(json.Marshal(map1)))
}

that returns this error:

tmp/sandbox100532722/main.go:13:33: multiple-value json.Marshal() in single-value context.

How do I fix this?

sensorario
  • 20,262
  • 30
  • 97
  • 159

1 Answers1

22

The string conversion you are trying to perform requires a single argument, but the json.Marshal function returns two ([]byte and error). You need to store the first return value and then do the conversion:

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    map1 := map[string]map[string]interface{}{}
    map2 := map[string]interface{}{}
    map2["map2item"] = "map2item"
    map1["map2"] = map2
    b, err := json.Marshal(map1)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(b))
}