Home > Backend Development > Golang > How to Use Multiple JSON Tags for a Single Struct Field in Go?

How to Use Multiple JSON Tags for a Single Struct Field in Go?

Mary-Kate Olsen
Release: 2024-12-17 03:25:25
Original
418 people have browsed it

How to Use Multiple JSON Tags for a Single Struct Field in Go?

Multiple JSON Tags for Struct Fields

In a scenario where you receive JSON data from a server and need to unmarshal it into a struct, you might encounter situations where you want to represent the same data with different JSON tags for different purposes.

Customizing JSON Tags

The JSON tag specifies the field name used in the JSON representation. By default, a field's tag is the same as the field name. However, you can customize the tags to change the names used when serializing or deserializing the struct.

Single JSON Tag

Using a single JSON tag is straightforward. Simply specify the desired tag as a string literal:

type Foo struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}
Copy after login

Multiple JSON Tags

As mentioned in the question, it's not possible to attach multiple JSON tags directly to a single field. However, there's a technique that allows you to work around this limitation.

Struct Casting

The given solution suggests using two structs that have the same field layout. For example:

type Foo struct {
    Name string
    Age  int
}

type Bar struct {
    Name string `json:"employee_name"`
    Age  int    `json:"-"`
}
Copy after login

Then, you can cast the Foo struct to a Bar struct to change the JSON tags. This technique is especially useful when you have a large number of fields:

foo := Foo{Name: "Sam", Age: 20}
bar := (*Bar)(unsafe.Pointer(&foo))
Copy after login

Caution

It's important to note that the second struct should be unexported to prevent it from being accessed outside your current package. This ensures that the casting is only performed as intended and not accidentally misused.

Example

The following code demonstrates the casting technique mentioned above:

package main

import (
    "encoding/json"
    "fmt"
)

type Foo struct {
    Name string
    Age  int
}

type Bar struct {
    Name string `json:"employee_name"`
    Age  int    `json:"-"`
}

func main() {
    foo := Foo{Name: "Sam", Age: 20}
    bar := (*Bar)(unsafe.Pointer(&foo))

    jsonBytes, err := json.Marshal(bar)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(string(jsonBytes))
}
Copy after login

This code successfully serializes the Foo struct data using the JSON tags defined in the Bar struct. It produces the following JSON output:

{"employee_name":"Sam"}
Copy after login

The above is the detailed content of How to Use Multiple JSON Tags for a Single Struct Field in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template