35

I want to create a map that I can transform into a json object such as

{
   "a": "apple",
   "b": 2
}

but golang specifies that the map be declare with types, so I can have map[string]string or map[string]int. How do I create a json object like the above?

Note: I won't know what data and/or types I need until runtime or when I need to create the json object. Therefore I can't just create an object like

type Foo struct {
    A string `json:"a"`
    B int `json:"b"`
}

2 Answers 2

68

You can always use interface{}to store any type. As the documentation in the encoding/json package says:

To unmarshal JSON into an interface value, Unmarshal unmarshals the JSON into the concrete value contained in the interface value. If the interface value is nil, that is, has no concrete value stored in it, Unmarshal stores one of these in the interface value:

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

Just do the following:

m := map[string]interface{}{"a":"apple", "b":2}
5
  • Can one of your values also be an array? m := map[string]interface{}{"a":"apple", "b":2, "c": ["foo", 2, "bar", false, {"baz": "bat", "moreFoo": 7}]} May 1, 2017 at 18:25
  • 2
    @BrantleyBeaird Sure! The empty interface (interface{}) holds any type, including arrays. Since your array contains arbitrary types in itself, it would become type []interface{}
    – ANisus
    May 2, 2017 at 9:15
  • 3
    Interface types (of which interface{} is one specifying no methods) can hold any underlying types, but as such they add runtime overhead in terms of memory use (additional type information is stored at the moment its value is converted to an interface value) and some processing. Reasonable price to pay for some of the flexibility you may be used to from a dynamic language, but not something to use when there is no reason to do so. Interface types can hold any underlying type except another interface type. This includes plain types like int, float64, but also arrays, maps, channels etc. Aug 2, 2017 at 5:57
  • How to make it right m := map[string][]interface{}{"a":"apple", "b":2, "c": ["foo", 2, "bar", false, {"baz": "bat", "moreFoo": 7}]} ? Apr 6, 2020 at 9:34
  • 2
    @ErtuğrulAltınboğa I was a bit unclear. For composite literals (structs, arrays, slices, and maps) you must first specify the type. It would be: m := map[string]interface{}{"a": "apple", "b": 2, "c": []interface{}{"foo", 2, "bar", false, map[string]interface{}{"baz": "bat", "moreFoo": 7}}}
    – ANisus
    Apr 6, 2020 at 9:49
6

To respond to the comments, I think it is easier to add type definitions for Map and Slice, then you don't have to worry about complex literal declarations:

package main
import "fmt"

type Map map[string]interface{}
type Slice []interface{}

func main() {
   m := Map{
      "a": "apple",
      "b": 2,
      "c": Slice{"foo", 2, "bar", false, Map{"baz": "bat", "moreFoo": 7}},
   }
   fmt.Println(m)
}

https://golang.org/ref/spec#Type_definitions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.