Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use json tags in anonymously nested struct?

I have the following anonymous struct

func wrapHal(selfHref string) interface{} {
    return struct {
        Links struct `json: "_links"`{ # does not work, returns error "expected expression"
            Self struct {
                Href string
            }
        }
    }{
        Links: struct {
            Self struct {
                Href string
            }
        }{
            Self: struct {
                Href string
            }{
                Href: selfHref,
            },
        },
    }
}

I want to know if it is possible to rename from Links to _links when I serialize to JSON using json.Marshal

Is it possible? If so, how?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The issue is with the base structure.

    type s struct {
        Links struct {
            Self struct {
                Href string
            }
        } `json:"_links"`
    }

is the correct syntax

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading