110

I want to convert a struct to map in Golang. It would also be nice if I could use the JSON tags as keys in the created map (otherwise defaulting to field name).

Edit Dec 14, 2020

Since structs repo was archived, you can use mapstructure instead.

Edit TL;DR version, Jun 15, 2015

If you want the fast solution for converting a structure to map, see the accepted answer, upvote it and use that package.

Happy coding! :)


Original Post

So far I have this function, I am using the reflect package but I don't understand well how to use the package, please bear with me.

func ConvertToMap(model interface{}) bson.M {
    ret := bson.M{}

    modelReflect := reflect.ValueOf(model)

    if modelReflect.Kind() == reflect.Ptr {
        modelReflect = modelReflect.Elem()
    }

    modelRefType := modelReflect.Type()
    fieldsCount := modelReflect.NumField()

    var fieldData interface{}

    for i := 0; i < fieldsCount; i++ {
        field := modelReflect.Field(i)

        switch field.Kind() {
        case reflect.Struct:
            fallthrough
        case reflect.Ptr:
            fieldData = ConvertToMap(field.Interface())
        default:
            fieldData = field.Interface()
        }

        ret[modelRefType.Field(i).Name] = fieldData
    }

    return ret
}

Also I looked at JSON package source code, because it should contain my needed implementation (or parts of it) but don't understand too much.

12
  • 1
    Is there a particular goal you're trying to achieve here? If you are dealing with the mgo/bson package (which seems possible due to the use of bson.M), can't it already perform a conversion from a struct similar to encoding/json? Commented May 11, 2014 at 6:33
  • 2
    It involves reflection, package reflect, and it is both slow and a royal pain to use; the json package is that way because using reflection is hard. My advice would be either to use something that already does the reflection parts for you (object-to-DB interfaces like gorp or mgo, builtin packages like json) or use (possibly repetitive) handwritten code to avoid reflection entirely. It's a situation where the approach that's natural and efficient in, say, JavaScript just isn't in Go. Commented May 11, 2014 at 6:44
  • 4
    github.com/mitchellh/mapstructure - but listen to James. In this case there is no reason to try and do this. Commented May 11, 2014 at 9:22
  • 3
    @eAbi First line of the README: "mapstructure is a Go library for decoding generic map values to structures and vice versa." (emphasis my own) Commented May 12, 2014 at 9:41
  • 2
    Now the mapstructure repo is also archived :-( Commented Aug 21, 2024 at 16:15

7 Answers 7

171

I also had need for something like this. I was using an internal package which was converting a struct to a map. I decided to open source it with other struct based high level functions. Have a look:

https://github.com/fatih/structs

It has support for:

  • Convert struct to a map
  • Extract the fields of a struct to a []string
  • Extract the values of a struct to a []values
  • Check if a struct is initialized or not
  • Check if a passed interface is a struct or a pointer to struct

You can see some examples here: http://godoc.org/github.com/fatih/structs#pkg-examples For example converting a struct to a map is a simple:

type Server struct {
    Name    string
    ID      int32
    Enabled bool
}

s := &Server{
    Name:    "gopher",
    ID:      123456,
    Enabled: true,
}

// => {"Name":"gopher", "ID":123456, "Enabled":true}
m := structs.Map(s)

The structs package has support for anonymous (embedded) fields and nested structs. The package provides to filter certain fields via field tags.

Sign up to request clarification or add additional context in comments.

13 Comments

wow @Fatih Arslan, this is huge. This practically provides "marshalling" structs to map[string]interface{}s just like encoding/json.Marhsal(), using struct tags and all. Amazing!
Here is a way to use "json" tag name instead of the "structs" tag name. github.com/fatih/structs/issues/25
Just a note that this project is now archived and has no maintenance.
And I do not want to add another dependency in my project
Don't forget to export the fields on your struct (Made a noob mistake)
|
98

From struct to map[string]interface{}

package main

import (
    "fmt"
    "encoding/json"
)

type MyData struct {
    One   int
    Two   string
    Three int
}

func main() {   
    in := &MyData{One: 1, Two: "second"}

    var inInterface map[string]interface{}
    inrec, _ := json.Marshal(in)
    json.Unmarshal(inrec, &inInterface)

    // iterate through inrecs
    for field, val := range inInterface {
            fmt.Println("KV Pair: ", field, val)
    }
}

go playground here

6 Comments

To keep it more simple, instead of using var inInterface interface{}, change it to var inInterface map[string]interface{}. See playground here: play.golang.org/p/woUiMzL_1X
This looks like a hack, I can't find anything clean or simple about this
There is a problem with this approach. In the resulting map, the int values were converted to float64, that is unexpected behavior. The inInterface["One"] value will be of type float64 instead of int.
Marshalling and then unmarshalling seems like a very inefficient way of converting data.
@d4nyll do you have another solution? Without using reflection (directly) of course.
|
26

Here is a function I've written in the past to convert a struct to a map, using tags as keys

// ToMap converts a struct to a map using the struct's tags.
//
// ToMap uses tags on struct fields to decide which fields to add to the
// returned map.
func ToMap(in interface{}, tag string) (map[string]interface{}, error){
    out := make(map[string]interface{})

    v := reflect.ValueOf(in)
    if v.Kind() == reflect.Ptr {
        v = v.Elem()
    }

    // we only accept structs
    if v.Kind() != reflect.Struct {
        return nil, fmt.Errorf("ToMap only accepts structs; got %T", v)
    }

    typ := v.Type()
    for i := 0; i < v.NumField(); i++ {
        // gets us a StructField
        fi := typ.Field(i)
        if tagv := fi.Tag.Get(tag); tagv != "" {
            // set key of map to value in struct field
            out[tagv] = v.Field(i).Interface()
        }
    }
    return out, nil
}

Runnable example here.

Note, if you have multiple fields with the same tag value, then you will obviously not be able to store them all within a map. It might be prudent to return an error if that happens.

4 Comments

Hmm, but I think it doesn't work with nested structs, right? I think the struct fields must be traversed recursively.
Your question didn't mention that. So you want a struct flattened into a map then?
Yes, a struct flattened into a map (so sub-structures would become a sub-map). You can achieve this by calling the funtion again if the field contains a structure, right?
I like this, but it is incomplete. It is not considering "-" and ",omitempty". Other than that it is really good.
15

I like the importable package for the accepted answer, but it does not translate my json aliases. Most of my projects have a helper function/class that I import.

Here is a function that solves my specific problem.


// Converts a struct to a map while maintaining the json alias as keys
func StructToMap(obj interface{}) (newMap map[string]interface{}, err error) {
    data, err := json.Marshal(obj) // Convert to a json string

    if err != nil {
        return
    }

    err = json.Unmarshal(data, &newMap) // Convert to a map
    return
}

And in the main, this is how it would be called...

package main

import (
    "fmt"
    "encoding/json"
    "github.com/fatih/structs"
)

type MyStructObject struct {
    Email string `json:"email_address"`
}

func main() {
    obj := &MyStructObject{Email: "[email protected]"}

    // My solution
    fmt.Println(StructToMap(obj)) // prints {"email_address": "[email protected]"}

    // The currently accepted solution
    fmt.Println(structs.Map(obj)) // prints {"Email": "[email protected]"}
}

1 Comment

It works, but it's not an optimal solution, double encoding is executed, shouldn't be used when performance is critical. About 10 times slower than creating a map and adding key/value manually.
4
package main

import (
    "fmt"
    "reflect"
)

type bill struct {
    N1 int
    N2 string
    n3 string
}

func main() {
    a := bill{4, "dhfthf", "fdgdf"}

    v := reflect.ValueOf(a)

    values := make(map[string]interface{}, v.NumField())

    for i := 0; i < v.NumField(); i++ {
        if v.Field(i).CanInterface() {
            values[v.Type().Field(i).Name] = v.Field(i).Interface()
        } else {
            fmt.Printf("sorry you have a unexported field (lower case) value you are trying to sneak past. I will not allow it: %v\n", v.Type().Field(i).Name)
        }
    }

    fmt.Println(values)

    passObject(&values)
}

func passObject(v1 *map[string]interface{}) {
    fmt.Println("yoyo")
}

1 Comment

this also works for structs coming from different packages. (note you won't have access to unexported fields)
1

I'm a bit late but I needed this kind of feature so I wrote this. Can resolve nested structs. By default, uses field names but can also use custom tags. A side effect is that if you set the tagTitle const to json, you could use the json tags you already have.

package main

import (
    "fmt"
    "reflect"
)

func StructToMap(val interface{}) map[string]interface{} {
    //The name of the tag you will use for fields of struct
    const tagTitle = "kelvin"

    var data map[string]interface{} = make(map[string]interface{})
    varType := reflect.TypeOf(val)
    if varType.Kind() != reflect.Struct {
        // Provided value is not an interface, do what you will with that here
        fmt.Println("Not a struct")
        return nil
    }

    value := reflect.ValueOf(val)
    for i := 0; i < varType.NumField(); i++ {
        if !value.Field(i).CanInterface() {
            //Skip unexported fields
            continue
        }
        tag, ok := varType.Field(i).Tag.Lookup(tagTitle)
        var fieldName string
        if ok && len(tag) > 0 {
            fieldName = tag
        } else {
            fieldName = varType.Field(i).Name
        }
        if varType.Field(i).Type.Kind() != reflect.Struct {
            data[fieldName] = value.Field(i).Interface()
        } else {
            data[fieldName] = StructToMap(value.Field(i).Interface())
        }

    }

    return data
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-5
map := Structpb.AsMap()

// map is the map[string]interface{}

3 Comments

How is this different from the accepted answer?
@FloLie it is pre-define library which will reduce your efforts and will give back your desire response.
Copy-pasting code is not helpful. Next time leave a explanation as to what the code does, where you found it, and why you chose it over other methods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.