You need to export the User.name field so that the json package can see it. Rename the name field to Name.

Copypackage main

import (
    "fmt"
    "encoding/json"
)

type User struct {
    Name string
}

func main() {
    user := &User{Name: "Frank"}
    b, err := json.Marshal(user)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(b))
}

Output:

Copy{"Name":"Frank"}
Answer from peterSO on Stack Overflow
🌐
Mholt
mholt.github.io › json-to-go
JSON-to-Go: Convert JSON to Go instantly
This tool instantly converts JSON into a Go type definition. Paste a JSON structure on the left and the equivalent Go type will be generated to the right, which you can paste into your program.
Discussions

How do you guys convert a json response to go structs?
There are tools that can create a struct out of a json object. https://mholt.github.io/json-to-go/ More on reddit.com
🌐 r/golang
68
56
January 14, 2024
A tool for generating structs based on json I have
https://mholt.github.io/json-to-go/ More on reddit.com
🌐 r/golang
7
3
September 26, 2021
What is the best way to handle json in Golang?
If the json is highly unstructured the unmarshal to map[string]any. For unknown structs use json.RawMessage More on reddit.com
🌐 r/golang
35
63
September 20, 2024
Converting an interface{} to a struct
Looks like you're describing tagged unions. There is an interresting answer on stackoverflow. This is ideal if the content is a child field and the type is on top level, you can just unmarshal the child once you know the type and avoid too much unmarshalling. Still a bit more parsing overhead than the customConverter solution, benchmarking will decide if parsing once to primitives is more expensive than parsing twice the child payload. the following comes from stackoverflow https://stackoverflow.com/questions/55994888/unmarshal-json-tagged-union-in-go : type Request struct { RequestId string Inputs []struct { Intent string Payload json.RawMessage } } var request Request err := json.Unmarshal(j, &request) if err != nil { log.Fatalln("error:", err) } for _, input := range request.Inputs { var payload interface{} switch input.Intent { case "action.devices.EXECUTE": payload = new(Execute) case "action.devices.QUERY": payload = new(Query) } err := json.Unmarshal(input.Payload, payload) if err != nil { log.Fatalln("error:", err) } // Do stuff with payload } More on reddit.com
🌐 r/golang
21
17
April 6, 2023
🌐
Go by Example
gobyexample.com › json
Go by Example: JSON
Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types · We’ll use these two structs to demonstrate encoding and decoding of custom types below
🌐
Bacancy Technology
bacancytechnology.com › qanda › golang › converting-go-struct-to-json
Streamlining JSON Conversion: Converting Go Struct to JSON
To convert a Go struct to JSON, you can use the json.Marshal() function from the encoding/json package. This function accepts a value of any type and returns its JSON representation as a byte slice.
🌐
The Trie Data Structure
drstearns.github.io › tutorials › gojson
Go Structs and JSON
How to encode structs into JSON and decode JSON into structs
🌐
Transform
transform.tools › json-to-go
JSON to Go Struct - Transform
to Go Struct · to GraphQL · to io-ts · to Java · to JSDoc · to JSON Schema · to Kotlin · to MobX-State-Tree Model · to Mongoose Schema · to MySQL · to React PropTypes · to Rust Serde · to Sarcastic · to Scala Case Class · to TOML · to TypeScript ·
🌐
GitHub
github.com › twpayne › go-jsonstruct
GitHub - twpayne/go-jsonstruct: Generate Go structs from multiple JSON or YAML objects. · GitHub
Handles JSON and YAML. Generates Go struct field names from camelCase, kebab-case, and snake_case object property names. Capitalizes common abbreviations (e.g. HTTP, ID, and URL) when generating Go struct field names to follow Go conventions, with the option to add your own abbreviations.
Starred by 363 users
Forked by 35 users
Languages   Go
Find elsewhere
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-in-go-1 › lessons › encoding-structs-into-json-in-go-1
Encoding Structs into JSON in Go
In this example, we define a Todo struct with fields Title, Done, and Description. Each field has a JSON tag that specifies the key name in the resulting JSON object. We create an instance of Todo and use json.Marshal to convert it into JSON. If successful, the JSON object is printed.
🌐
Soham Kamani
sohamkamani.com › golang › json
A Complete Guide to JSON in Golang (With Examples)
September 11, 2023 - This post will describe how to marshal and unmarshal JSON in Go. We will learn how to convert from JSON raw data into Go types like structs, maps, and slices
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-in-go-1 › lessons › decoding-json-into-structs-in-go
Decoding JSON into Structs in Go
The struct tags (json:"title", etc.) map the JSON keys to Go struct fields. ... This function fetches JSON data from the given API URL and returns the raw JSON response body.
🌐
Medium
oofnivek.medium.com › golang-array-of-struct-to-json-3bf71492a8dd
Golang Array Of Struct To JSON - Kevin FOO - Medium
September 1, 2021 - package main import ( "log" ... output as below. ... In order to convert to JSON, the back tick enclosed description for the struct field is very important....
🌐
Reddit
reddit.com › r/golang › how do you guys convert a json response to go structs?
r/golang on Reddit: How do you guys convert a json response to go structs?
January 14, 2024 -

I have been practicing writing go for the last 20-25 days. I’m getting used to the syntax and everything. But, when integrating any api, the most difficult part is not making the api call. It is the creation of the response object as a go struct especially when the api response is big. Am I missing some tool that y’all been using?

🌐
Better Stack
betterstack.com › community › guides › scaling-go › json-in-go
A Comprehensive Guide to Using JSON in Go | Better Stack Community
There are two key terminologies to note when working with JSON in Go: Marshalling: the act of converting a Go data structure into valid JSON.
🌐
YourBasic
yourbasic.org › golang › json-example
How to use JSON with Go [best practices] · YourBasic Go
April 9, 2018 - yourbasic.org/golang · The JSON data-interchange format is easy for humans to read and write, and efficient for machines to parse and generate. Default types · Encode (marshal) struct to JSON · Pretty print · Decode (unmarshal) JSON to struct · Arbitrary objects and arrays ·
🌐
Go
go.dev › blog › json
JSON and Go - The Go Programming Language
Therefore only the exported fields of a struct will be present in the JSON output. To decode JSON data we use the Unmarshal function.
🌐
GitHub
gist.github.com › miguelmota › 04252b346d281b93f047bf9a20f73f04
Golang JSON Marshal (struct to string) (M for "Make json") and Unmarshal (string to struct) (U for "Unmake json") example · GitHub
Golang JSON Marshal (struct to string) (M for "Make json") and Unmarshal (string to struct) (U for "Unmake json") example - json_marshal_unmarshal.go
🌐
Medium
medium.com › @okesolakofo › generating-json-data-from-go-golang-structs-with-go2json-a013120d5d6
Generating JSON data from Go (Golang) structs with Go2JSON | by Kofo Okesola | Medium
February 5, 2022 - package maintype Example struct { Name string Address string Phone string Email string IntField int `json:"field_1"` StringField string Custom []CustomStruct CustomEmbedded struct { First string } `json:"embedded"` }type CustomStruct struct { Truthy bool ListString []string } To do this you would have probably had to painstakingly create and fill each field in your JSON by hand, or probably with a JSON editor like JsonEditor. ... GO2JSON initially started out as a way for me to explore the AST (abstract syntax tree) of Golang as well as potential code generation, but after a few frustrating JSON API endpoint testing for other projects, I turned it to GO2JSON.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-struct-tags-in-go
How To Use Struct Tags in Go | DigitalOcean
March 24, 2026 - Learn how to use struct tags in Go for JSON, XML, database mapping, and validation. Includes syntax rules, reflection examples, and custom tag patterns.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-json-in-go
How To Use JSON in Go | DigitalOcean
March 28, 2022 - While using a map[string]interface{} to marshal JSON data can be very flexible, it can also become a hassle if you need to send the same data in multiple places. If you copy this data to more than one place in your code, it can be easy to accidentally mistype a field name, or assign the incorrect data to a field. For times like these, it can be beneficial to use a struct type to represent the data you’re converting to JSON.