33

What is the way to get the json field names of this struct ?

type example struct {
    Id          int `json:"id"`
    CreatedAt   string `json:"created_at"`
    Tag         string `json:"tag"`
    Text        string `json:"text"`
    AuthorId    int `json:"author_id"`
}

I try to print the fields with this function :

func (b example) PrintFields() {
    val := reflect.ValueOf(b)
    for i := 0; i < val.Type().NumField(); i++ {
        fmt.Println(val.Type().Field(i).Name)
    }
}

Of course I get :

Id
CreatedAt
Tag
Text
AuthorId

But I would like something like :

id
created_at
tag
text
author_id
lambher
  • 433
  • 1
  • 4
  • 6
  • Check out this answer (it contains an example to get tag values): [What are the use(s) for tags in Go?](http://stackoverflow.com/questions/10858787/what-are-the-uses-for-tags-in-go/30889373#30889373) – icza Nov 29 '16 at 12:00

7 Answers7

42

You use the StructTag type to get the tags. The documentation I linked has examples, look them up, but your code could be something like

func (b example) PrintFields() {
  val := reflect.ValueOf(b)
  for i := 0; i < val.Type().NumField(); i++ {
     fmt.Println(val.Type().Field(i).Tag.Get("json"))
  }
}

NOTE The json tag format supports more than just field names, such as omitempty or string, so if you need an approach that takes care of that too, further improvements to the PrintFields function should be made:

  1. we need to check whether the json tag is - (i.e. json:"-")
  2. we need to check if name exists and isolate it

Something like this:

func (b example) PrintFields() {
  val := reflect.ValueOf(b)
  for i := 0; i < val.Type().NumField(); i++ {
    t := val.Type().Field(i)
    fieldName := t.Name

    switch jsonTag := t.Tag.Get("json"); jsonTag {
    case "-":
    case "":
        fmt.Println(fieldName)
    default:
        parts := strings.Split(jsonTag, ",")
        name := parts[0]
        if name == "" {
            name = fieldName
        }
        fmt.Println(name)
    }
  }
}
ain
  • 22,394
  • 3
  • 54
  • 74
  • 3
    Please note that the `json` tag supports more than just the name of the property, such as `,omitempty` and your code is going to fail that by returning more than just the name of the field. – Victor Nov 08 '18 at 15:14
5

Instead of using StructField's Name, you can use Tag to get a StructTag object. See: https://golang.org/pkg/reflect/#StructTag

Then you can use StructTag's Get or Lookup methods to get the json tag:

Using Get:

func (b example) PrintFields() {
    val := reflect.ValueOf(b)
    for i := 0; i < val.Type().NumField(); i++ {
        // prints empty line if there is no json tag for the field
        fmt.Println(val.Type().Field(i).Tag.Get("json"))
    }
}

Using Lookup:

func (b example) PrintFields() {
    val := reflect.ValueOf(b)
    for i := 0; i < val.Type().NumField(); i++ {
        // skips fields without json tag
        if tag, ok := val.Type().Field(i).Tag.Lookup("json"); ok {
            fmt.Println(tag)
        }
    }
}
jussius
  • 3,114
  • 15
  • 21
3

Use:

func (b example) PrintFields() {
    val := reflect.ValueOf(b)
    t := val.Type()
    for i := 0; i < t.NumField(); i++ {
        fmt.Println(t.Field(i).Tag.Get("json"))
    }
}

See it in playground.

sadlil
  • 3,077
  • 5
  • 23
  • 36
  • As `val` is not used in the snippet this can now be simplified to `t := reflect.TypeOf(b)` - https://go.dev/play/p/tqtlOkmA20x – Marcus Jul 14 '22 at 14:50
3

an updated version with a generic interface as parameter:

func PrintFields(b interface{}) {
    val := reflect.ValueOf(b)
    for i := 0; i < val.Type().NumField(); i++ {
        t := val.Type().Field(i)
        fieldName := t.Name

        if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
            // check for possible comma as in "...,omitempty"
            var commaIdx int
            if commaIdx = strings.Index(jsonTag, ","); commaIdx < 0 {
                commaIdx = len(jsonTag)
            }
            fieldName = jsonTag[:commaIdx]
        }
        fmt.Println(fieldName)
    }
}
ChuckM
  • 31
  • 2
2
func (e example) GetJsonField() []string {
    b := example{}
    marshaled, _ := json.Marshal(b)
    m := make(map[string]interface{})
    _ = json.Unmarshal(marshaled, &m)
    
    result := make([]string, 0)
    for k := range m {
        result = append(result, k)
    }
    return result
}
user2352151
  • 81
  • 1
  • 5
1

Not the Name you are looking for. What you looking is the Tag

func (b example) PrintFields() {
    val := reflect.ValueOf(b)
    for i := 0; i < val.Type().NumField(); i++ {
        fmt.Println(val.Type().Field(i).Tag.Get("json"))
    }
}
Wendy Adi
  • 1,397
  • 3
  • 18
  • 26
0

Get Json field name Using Struct Field Name

val := reflect.ValueOf(structobject)    
field, _ := val.Type().FieldByName("Id")
fmt.Println(field.Tag.Get("json"))
Saurabh
  • 1
  • 1