28

It it possible in Golang to use more than one name for a JSON struct tag ?

type Animation struct {
    Name    string  `json:"name"`
    Repeat  int     `json:"repeat"`
    Speed   uint    `json:"speed"`
    Pattern Pattern `json:"pattern",json:"frames"`
}
2
  • 6
    Voting to re-open. The question linked as duplicate with answer is for assigning multiple different name tags in a struct. The OP is asking for how to assign multiple json tags... which is a different question. Commented Dec 11, 2019 at 5:08
  • Dup, see good answers here: stackoverflow.com/questions/37118263/… Commented May 7, 2020 at 4:47

2 Answers 2

20

you can use multiple json tag with third part json lib like github.com/json-iterator/go coding like below:

package main

import (
    "fmt"
    "github.com/json-iterator/go"
)

type TestJson struct {
    Name string `json:"name" newtag:"newname"`
    Age  int    `json:"age" newtag:"newage"`
}

func main() {

    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    data := TestJson{}
    data.Name = "zhangsan"
    data.Age = 22
    byt, _ := json.Marshal(&data)
    fmt.Println(string(byt))

    var NewJson = jsoniter.Config{
        EscapeHTML:             true,
        SortMapKeys:            true,
        ValidateJsonRawMessage: true,
        TagKey:                 "newtag",
    }.Froze()

    byt, _ = NewJson.Marshal(&data)
    fmt.Println(string(byt))
}

output:

    {"name":"zhangsan","age":22}
    {"newname":"zhangsan","newage":22}
Sign up to request clarification or add additional context in comments.

2 Comments

Your solution answers for when somebody wants to add multiple different tags to different packages. ie in your case json and newtag. OP asked for a usecase when he/she wants to target the same package. ie. In json if you have to specify 2 different properties. Usecases for example in json formatting options, ORMs etc. Your solution is pretty much just working around that problem. no?
Is there any pkg for yaml ?
17

See How to define multiple name tags in a struct on how you can define multiple tags on one struct field.

You can also use a type Info map[string]interface{} instead of your struct.

Or you can use both types in your structure, and make method Details() which will return right pattern.

type Animation struct {
    Name    string  `json:"name"`
    Repeat  int     `json:"repeat"`
    Speed   uint    `json:"speed"`
    Pattern Pattern `json:"pattern"`
    Frame   Pattern `json:"frames"`
}

func (a Animation) Details() Pattern {
    if a.Pattern == nil {
        return a.Frame
    }
    return a.Pattern
}

2 Comments

Thanks. I needed something like this for retro compatibility. This is good.
I think this is a good answer, but IIUC you cannot use multiple json name tags to any useful effect. For example json:"pattern" json:"frames" will not tell Unmarshal to unmarshal either pattern or frames. So maybe it's worth noting after the link "but this doesn't work the way you want"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.