{"id":818,"date":"2026-10-08T17:00:00","date_gmt":"2026-10-08T11:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-json-programs\/"},"modified":"2026-11-29T01:29:30","modified_gmt":"2026-11-28T19:59:30","slug":"go-json-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-json-programs\/","title":{"rendered":"Go JSON Programs with Output: Marshal, Unmarshal and Struct Tags"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go JSON programs turn Go structs into JSON text and turn JSON text back into Go values, which is how almost every Go API talks to browsers, mobile apps and other services. In this post, you will write fourteen JSON programs with their exact output, and each one explains its logic in the code comments. We end with the JSON programs interviewers ask most, such as parsing an API response and summing values from it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should know structs and tags from our <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs<\/a> and files from our <a href=\"https:\/\/smitabharat.com\/blog\/go-file-programs\/\">Go file programs<\/a>. For the theory, read <a href=\"https:\/\/smitabharat.com\/blog\/go-json-encoding-decoding\/\">Go JSON encoding and decoding<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go JSON Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go JSON programs use the <code>encoding\/json<\/code> package. Marshalling turns a Go value into JSON bytes, and unmarshalling reads JSON bytes into a Go value. Struct tags like <code>json:\"name\"<\/code> decide the JSON field names.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of JSON as a shipping label that every system can read. Your Go struct is the parcel. Marshalling prints the label, and unmarshalling reads a label that someone else wrote and packs the parcel to match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go JSON Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JSON is the common language of the web. For example, a REST API returns orders as JSON, a mobile app sends a login form as JSON and a config file is often JSON. So Go JSON programs are needed in nearly every backend job.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>APIs:<\/strong> send and receive request and response bodies.<\/li>\n\n\n<li><strong>Configs:<\/strong> load settings from JSON files.<\/li>\n\n\n<li><strong>Integrations:<\/strong> read data from payment, SMS and email services.<\/li>\n\n\n<li><strong>Interviews:<\/strong> tags, <code>omitempty<\/code>, unknown fields and <code>map[string]any<\/code> are common questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go JSON Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you marshal, Go looks at each exported field, reads its tag, and writes a JSON key and value. When you unmarshal, Go matches each JSON key to a field by tag or name, ignoring case, and converts the value to the field type.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-1024x640.png\" alt=\"Go JSON programs diagram showing a struct marshalled into JSON and unmarshalled back\" class=\"wp-image-816\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">json.Marshal turns a struct into JSON using its tags, and json.Unmarshal fills a struct from JSON text.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows how Go types map to JSON.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Go type<\/th><th>JSON type<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>string<\/td><td>string<\/td><td>&#8220;Asha&#8221;<\/td><\/tr><tr><td>int, float64<\/td><td>number<\/td><td>42, 99.5<\/td><\/tr><tr><td>bool<\/td><td>true or false<\/td><td>true<\/td><\/tr><tr><td>struct<\/td><td>object<\/td><td>{&#8220;id&#8221;:1}<\/td><\/tr><tr><td>slice, array<\/td><td>array<\/td><td>[1,2,3]<\/td><\/tr><tr><td>map[string]T<\/td><td>object<\/td><td>{&#8220;pen&#8221;:10}<\/td><\/tr><tr><td>nil pointer, nil slice<\/td><td>null<\/td><td>null<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go JSON Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go JSON programs marshal and unmarshal simple structs. If JSON is new to you, start with Program 1, which turns a plain list into JSON.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Turn a List of Names into JSON<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\" \/\/ encoding\/json turns Go values into JSON and back\n    \"fmt\"           \/\/ fmt prints text on the screen\n)\n\nfunc main() {\n    \/\/ A slice is a list of values. Here it holds three names.\n    \/\/ := creates a new variable and stores the value in it.\n    names := []string{\"Asha\", \"Ravi\", \"Meera\"}\n\n    \/\/ json.Marshal turns the list into JSON.\n    \/\/ It returns the JSON as bytes, plus err (the error value, nil if all went well).\n    data, err := json.Marshal(names)\n    if err != nil {\n        fmt.Println(\"error:\", err)\n        return\n    }\n\n    \/\/ string() turns the bytes into text we can print.\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[\"Asha\",\"Ravi\",\"Meera\"]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, you build a normal Go list of names. Then <code>json.Marshal<\/code> turns it into a JSON array with square brackets and quotes. Finally, <code>string(data)<\/code> makes the bytes printable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Marshal a Struct to JSON<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Product is one item in a shop.\n\/\/ Tags set the JSON names. Without them, JSON would use \"ID\", \"Name\" and so on.\ntype Product struct {\n    ID    int     `json:\"id\"`\n    Name  string  `json:\"name\"`\n    Price float64 `json:\"price\"`\n}\n\nfunc main() {\n    product := Product{ID: 7, Name: \"Notebook\", Price: 45.5}\n\n    \/\/ Marshal returns JSON as a byte slice plus an error.\n    data, err := json.Marshal(product)\n    if err != nil {\n        fmt.Println(\"error:\", err)\n        return\n    }\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"id\":7,\"name\":\"Notebook\",\"price\":45.5}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>json.Marshal<\/code> walks through each field of the struct. Only exported fields (capital first letter) are marshalled. The tag text after <code>json:<\/code> becomes the key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Pretty Print with MarshalIndent<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ User has a name and a list of roles.\ntype User struct {\n    Name  string   `json:\"name\"`\n    Roles []string `json:\"roles\"`\n}\n\nfunc main() {\n    user := User{Name: \"Ravi\", Roles: []string{\"admin\", \"editor\"}}\n\n    \/\/ The second argument is a prefix for each line, the third is the indent (two spaces).\n    data, _ := json.MarshalIndent(user, \"\", \"  \") \/\/ the _ ignores the error to keep it short\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"Ravi\",\n  \"roles\": [\n    \"admin\",\n    \"editor\"\n  ]\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>MarshalIndent<\/code> works like <code>Marshal<\/code>, but it puts each value on its own line. Use it for logs and files people will read. APIs usually use plain <code>Marshal<\/code>, which is smaller.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Read a Simple JSON Text<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Weather holds the two values we want from the JSON text.\ntype Weather struct {\n    City        string `json:\"city\"`\n    Temperature int    `json:\"temp\"` \/\/ the JSON key \"temp\" fills this field\n}\n\nfunc main() {\n    \/\/ Backticks make a raw string, so the double quotes inside need no escaping.\n    body := `{\"city\": \"Pune\", \"temp\": 31}`\n\n    var weather Weather \/\/ an empty Weather, ready to be filled\n\n    \/\/ Unmarshal reads the JSON and fills weather. &amp; gives Unmarshal its address.\n    err := json.Unmarshal([]byte(body), &amp;weather)\n    if err != nil {\n        fmt.Println(\"error:\", err)\n        return\n    }\n    fmt.Println(weather.City, \"is\", weather.Temperature, \"degrees today\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Pune is 31 degrees today<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the reverse of Program 1. <code>json.Unmarshal<\/code> reads each JSON key and finds the field with the matching tag. So &#8220;temp&#8221; goes into <code>Temperature<\/code>, even though the names differ.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Unmarshal JSON into a Struct<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Order is a shop order from a client.\ntype Order struct {\n    ID     int     `json:\"id\"`\n    Amount float64 `json:\"amount\"`\n    Paid   bool    `json:\"paid\"`\n}\n\nfunc main() {\n    body := []byte(`{\"id\": 501, \"amount\": 1299.0, \"paid\": true, \"coupon\": \"DIWALI\"}`)\n\n    var order Order\n    \/\/ Pass a pointer, so Unmarshal can fill the struct.\n    err := json.Unmarshal(body, &amp;order)\n    if err != nil {\n        fmt.Println(\"error:\", err)\n        return\n    }\n    fmt.Printf(\"%+v\\n\", order) \/\/ %+v prints field names with their values\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{ID:501 Amount:1299 Paid:true}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The extra &#8220;coupon&#8221; key is ignored, because <code>Order<\/code> has no matching field. You must pass <code>&amp;order<\/code>, or Go cannot change your variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tag Options in Go JSON Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go JSON programs, tags can do more than rename fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: omitempty, Skip and String Options<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Account shows three tag options.\ntype Account struct {\n    Email    string `json:\"email\"`\n    Phone    string `json:\"phone,omitempty\"` \/\/ left out when empty\n    Password string `json:\"-\"`               \/\/ never in JSON\n    Balance  int    `json:\"balance,string\"`  \/\/ number written as a string\n}\n\nfunc main() {\n    account := Account{Email: \"asha@example.com\", Password: \"secret\", Balance: 2500}\n    data, _ := json.Marshal(account)\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"email\":\"asha@example.com\",\"balance\":\"2500\"}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>omitempty<\/code> hides the empty phone, and <code>-<\/code> keeps the password out of the JSON completely. The <code>string<\/code> option is useful when another system expects numbers inside quotes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Nested Structs and Slices<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Item is one thing in the cart.\ntype Item struct {\n    Name     string `json:\"name\"`\n    Quantity int    `json:\"qty\"`\n}\n\n\/\/ Address is where the cart is shipped.\ntype Address struct {\n    City string `json:\"city\"`\n}\n\n\/\/ Cart holds a user, a list of items and a shipping address.\ntype Cart struct {\n    User  string  `json:\"user\"`\n    Items []Item  `json:\"items\"`\n    Ship  Address `json:\"ship\"`\n}\n\nfunc main() {\n    body := `{\"user\":\"Meera\",\"items\":[{\"name\":\"pen\",\"qty\":3},{\"name\":\"bag\",\"qty\":1}],\"ship\":{\"city\":\"Pune\"}}`\n    var cart Cart\n    err := json.Unmarshal([]byte(body), &amp;cart)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    \/\/ The JSON array became a Go slice, so we can loop over it with range.\n    total := 0\n    for _, item := range cart.Items {\n        total += item.Quantity\n    }\n    fmt.Println(cart.User, \"ships to\", cart.Ship.City, \"with\", total, \"items\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Meera ships to Pune with 4 items<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">JSON objects become structs, and JSON arrays become slices. The loop then adds up the quantity of each item. Nesting works to any depth.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flexible Go JSON Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes Go JSON programs do not know the JSON shape in advance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Unknown JSON into map[string]any<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    body := `{\"name\":\"Kiran\",\"age\":31,\"skills\":[\"go\",\"sql\"],\"active\":true}`\n\n    \/\/ any means \"a value of any type\", so this map can hold anything.\n    var data map[string]any\n    json.Unmarshal([]byte(body), &amp;data)\n\n    \/\/ Every JSON number becomes float64 in a map[string]any.\n    \/\/ .(float64) is a type assertion: it says \"I know this value is a float64\".\n    age := data[\"age\"].(float64)\n    skills := data[\"skills\"].([]any) \/\/ a JSON array becomes []any\n    fmt.Println(data[\"name\"], int(age), len(skills), data[\"active\"])\n\n    \/\/ %T prints the type of a value.\n    fmt.Printf(\"%T %T\\n\", data[\"age\"], data[\"skills\"])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Kiran 31 2 true\nfloat64 []interface {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the most common JSON surprise: numbers come back as <code>float64<\/code>, and arrays as <code>[]any<\/code>. Use type assertions, as in our <a href=\"https:\/\/smitabharat.com\/blog\/go-interface-programs\/\">Go interface programs<\/a>, to read them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Stream JSON with json.Decoder<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"strings\"\n)\n\n\/\/ Event is one action a user took.\ntype Event struct {\n    Type string `json:\"type\"`\n    User string `json:\"user\"`\n}\n\nfunc main() {\n    \/\/ Many JSON objects, one per line, like a log file or HTTP stream.\n    stream := strings.NewReader(`{\"type\":\"login\",\"user\":\"asha\"}\n{\"type\":\"buy\",\"user\":\"ravi\"}\n{\"type\":\"logout\",\"user\":\"asha\"}`)\n\n    decoder := json.NewDecoder(stream)\n    for decoder.More() { \/\/ More reports whether another value is waiting\n        var event Event\n        err := decoder.Decode(&amp;event) \/\/ read the next JSON object\n        if err != nil {\n            fmt.Println(\"error:\", err)\n            break\n        }\n        fmt.Println(event.User, \"-&gt;\", event.Type)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>asha -&gt; login\nravi -&gt; buy\nasha -&gt; logout<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The decoder reads one JSON object on each <code>Decode<\/code> call. It works with any <code>io.Reader<\/code>, such as a file or an HTTP request body. Because it reads one value at a time, it handles big streams with little memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Reject Unknown Fields<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"strings\"\n)\n\n\/\/ Signup is the only data we accept in a sign-up request.\ntype Signup struct {\n    Email string `json:\"email\"`\n}\n\nfunc main() {\n    body := `{\"email\":\"a@b.com\",\"isAdmin\":true}`\n    decoder := json.NewDecoder(strings.NewReader(body))\n    decoder.DisallowUnknownFields() \/\/ fail instead of silently ignoring extra keys\n\n    var signup Signup\n    err := decoder.Decode(&amp;signup)\n    fmt.Println(err)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>json: unknown field \"isAdmin\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DisallowUnknownFields<\/code> makes the decoder strict. So the extra &#8220;isAdmin&#8221; key causes an error instead of being skipped. This protects APIs from typos and from clients sending fields you never expected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Custom MarshalJSON for Money<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Rupees stores money in paise to avoid float errors.\ntype Rupees int64\n\n\/\/ MarshalJSON decides exactly how Rupees look in JSON.\nfunc (amount Rupees) MarshalJSON() ([]byte, error) {\n    rupees := amount \/ 100 \/\/ whole rupees\n    paise := amount % 100  \/\/ % gives the remainder, the paise part\n    \/\/ %02d always prints two digits, so 5 paise becomes \"05\".\n    text := fmt.Sprintf(`\"Rs %d.%02d\"`, rupees, paise)\n    return []byte(text), nil\n}\n\n\/\/ Invoice has an ID and a total amount.\ntype Invoice struct {\n    ID    int    `json:\"id\"`\n    Total Rupees `json:\"total\"`\n}\n\nfunc main() {\n    data, _ := json.Marshal(Invoice{ID: 9, Total: 149950})\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"id\":9,\"total\":\"Rs 1499.50\"}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>json.Marshal<\/code> reaches the <code>Total<\/code> field, it calls our <code>MarshalJSON<\/code> method. The method splits the amount into rupees and paise and builds a quoted string. There is also <code>UnmarshalJSON<\/code> for the reverse direction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Go JSON Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go JSON programs are the ones interviewers ask most.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Parse an API Response and Sum Values<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Sale is one product and how much it sold.\ntype Sale struct {\n    Product string  `json:\"product\"`\n    Sales   float64 `json:\"sales\"`\n}\n\n\/\/ APIResponse is the full JSON reply from the server.\ntype APIResponse struct {\n    Status string `json:\"status\"`\n    Data   []Sale `json:\"data\"`\n}\n\nfunc main() {\n    body := `{\"status\":\"ok\",\"data\":[\n        {\"product\":\"pen\",\"sales\":1200.5},\n        {\"product\":\"book\",\"sales\":3400},\n        {\"product\":\"bag\",\"sales\":899.5}]}`\n\n    var response APIResponse\n    err := json.Unmarshal([]byte(body), &amp;response)\n    if err != nil {\n        fmt.Println(\"bad JSON:\", err)\n        return\n    }\n\n    \/\/ Loop over the data slice, add up sales and remember the best seller.\n    total := 0.0\n    best := \"\"\n    bestSales := -1.0\n    for _, sale := range response.Data {\n        total += sale.Sales\n        if sale.Sales &gt; bestSales {\n            bestSales = sale.Sales\n            best = sale.Product\n        }\n    }\n    fmt.Printf(\"status=%s total=%.2f best=%s\\n\", response.Status, total, best)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>status=ok total=5500.00 best=book<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>Unmarshal<\/code> fills the response struct, including the list of sales. Then the loop adds each sale to <code>total<\/code>. It also keeps the product with the highest sales, which is the book.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Handle Bad JSON Gracefully<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"errors\"\n    \"fmt\"\n)\n\n\/\/ Config holds the server port.\ntype Config struct {\n    Port int `json:\"port\"`\n}\n\nfunc main() {\n    inputs := []string{`{\"port\": 8080}`, `{\"port\": \"8080\"}`, `{\"port\": 8080`}\n    for _, input := range inputs {\n        var config Config\n        err := json.Unmarshal([]byte(input), &amp;config)\n\n        \/\/ errors.As checks the kind of error and fills these variables.\n        var typeErr *json.UnmarshalTypeError \/\/ a value had the wrong type\n        var syntaxErr *json.SyntaxError      \/\/ the JSON text itself is broken\n        switch {\n        case err == nil:\n            fmt.Println(\"ok, port\", config.Port)\n        case errors.As(err, &amp;typeErr):\n            fmt.Println(\"wrong type for field\", typeErr.Field)\n        case errors.As(err, &amp;syntaxErr):\n            fmt.Println(\"syntax error:\", err)\n        default:\n            fmt.Println(\"other error:\", err)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ok, port 8080\nwrong type for field port\nsyntax error: unexpected end of JSON input<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second input sends the port as text, so Go returns an <code>UnmarshalTypeError<\/code>. The third input is cut off, so Go returns a <code>SyntaxError<\/code>. Checking the error type lets an API return a helpful message to the client.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Convert a Map to Sorted JSON<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    stock := map[string]int{\"pen\": 40, \"bag\": 5, \"book\": 12}\n\n    \/\/ encoding\/json sorts map keys, so the output is always the same.\n    data, _ := json.Marshal(stock)\n    fmt.Println(string(data))\n\n    \/\/ Round trip: JSON back to a map.\n    var back map[string]int\n    json.Unmarshal(data, &amp;back)\n    fmt.Println(back[\"book\"], len(back))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"bag\":5,\"book\":12,\"pen\":40}\n12 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike printing a map with a <code>range<\/code> loop, <code>json.Marshal<\/code> always sorts map keys. That makes JSON output stable for tests and caches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Struct vs map[string]any in Go JSON Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go JSON programs can decode JSON into a struct or into a generic map. The diagram and table below compare them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-1024x640.png\" alt=\"Go JSON programs comparison of structs and map[string]any\" class=\"wp-image-817\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Structs give type safety and clear code, while map[string]any handles JSON whose shape you do not know.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Struct<\/th><th>map[string]any<\/th><\/tr><\/thead><tbody><tr><td>Know the shape first<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Type safety<\/td><td>Checked by the compiler<\/td><td>Needs type assertions<\/td><\/tr><tr><td>Numbers<\/td><td>Your chosen type<\/td><td>Always float64<\/td><\/tr><tr><td>Code readability<\/td><td>High<\/td><td>Low, many assertions<\/td><\/tr><tr><td>Best for<\/td><td>APIs you control or document<\/td><td>Unknown or changing JSON<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So use structs whenever you can, and fall back to <code>map[string]any<\/code> only for truly unknown data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go JSON Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lower case fields:<\/strong> unexported fields never appear in JSON, so start them with a capital letter.<\/li>\n\n\n<li><strong>Passing a value to Unmarshal:<\/strong> you must pass a pointer, such as <code>&amp;order<\/code>.<\/li>\n\n\n<li><strong>Expecting ints from map[string]any:<\/strong> JSON numbers become <code>float64<\/code>.<\/li>\n\n\n<li><strong>Ignoring errors:<\/strong> bad JSON leaves your struct half filled, so always check <code>err<\/code>.<\/li>\n\n\n<li><strong>Leaking secrets:<\/strong> add <code>json:\"-\"<\/code> to passwords and tokens.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go JSON Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Always use tags<\/strong>, so JSON names stay stable even if you rename Go fields.<\/li>\n\n\n<li><strong>Use json.Decoder<\/strong> for HTTP bodies and files, since it reads from a stream.<\/li>\n\n\n<li><strong>Turn on DisallowUnknownFields<\/strong> for strict APIs.<\/li>\n\n\n<li><strong>Store money as integers<\/strong>, and format it in a custom <code>MarshalJSON<\/code> if needed.<\/li>\n\n\n<li><strong>Use omitempty<\/strong> for optional fields, so responses stay small.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go JSON Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers ask these Go JSON programs more than any other JSON questions. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Marshal and Tag Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Person has one field without a tag, one unexported field and one optional field.\ntype Person struct {\n    Name  string\n    age   int    \/\/ lower case, so it is unexported\n    Email string `json:\"email,omitempty\"`\n}\n\nfunc main() {\n    data, _ := json.Marshal(Person{Name: \"Asha\", age: 30})\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"Name\":\"Asha\"}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Name<\/code> has no tag, so its JSON key is &#8220;Name&#8221;. The unexported <code>age<\/code> is skipped, and the empty <code>Email<\/code> is left out by <code>omitempty<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Box has two lists, one with omitempty.\ntype Box struct {\n    Items []string `json:\"items\"`\n    Tags  []string `json:\"tags,omitempty\"`\n}\n\nfunc main() {\n    nilItems, _ := json.Marshal(Box{})                    \/\/ Items is nil\n    emptyItems, _ := json.Marshal(Box{Items: []string{}}) \/\/ Items is an empty list\n    fmt.Println(string(nilItems))\n    fmt.Println(string(emptyItems))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"items\":null}\n{\"items\":[]}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A nil slice becomes <code>null<\/code>, while an empty slice becomes <code>[]<\/code>. Front-end developers often prefer <code>[]<\/code>, so set empty slices on purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. Does Unmarshal match keys case-sensitively?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. If no exact match exists, Go matches keys to field names or tags ignoring case. So &#8220;EMAIL&#8221; fills a field tagged <code>json:\"email\"<\/code>. Use exact names anyway, because other languages may be stricter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Unmarshal Questions in Go JSON Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Profile uses a pointer, so \"missing\" and \"zero\" look different.\ntype Profile struct {\n    Age *int `json:\"age\"` \/\/ *int is a pointer to an int, or nil\n}\n\nfunc main() {\n    var missing, zero Profile\n    json.Unmarshal([]byte(`{}`), &amp;missing)\n    json.Unmarshal([]byte(`{\"age\":0}`), &amp;zero)\n    fmt.Println(missing.Age == nil, *zero.Age) \/\/ *zero.Age reads the int the pointer points to\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A pointer field tells &#8220;missing&#8221; (nil) apart from &#8220;sent as 0&#8221;. This is the same idea as in our <a href=\"https:\/\/smitabharat.com\/blog\/go-pointer-programs\/\">Go pointer programs<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    good := json.Valid([]byte(`{\"a\":1}`)) \/\/ Valid only checks the JSON, it does not decode it\n    bad := json.Valid([]byte(`{a:1}`))\n    fmt.Println(good, bad)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">JSON keys must be in double quotes, so <code>{a:1}<\/code> is not valid JSON. <code>json.Valid<\/code> is a quick check before you decode.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    var value any\n    json.Unmarshal([]byte(`12345678901234567890`), &amp;value)\n    fmt.Printf(\"%T %v\\n\", value, value) \/\/ %T is the type, %v is the value\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>float64 1.2345678901234567e+19<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Big numbers lose precision as <code>float64<\/code>. For IDs like this, use <code>dec.UseNumber()<\/code> or decode into a string or <code>int64<\/code> field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    var numbers []int\n    err := json.Unmarshal([]byte(`[1, 2, 3]`), &amp;numbers)\n    fmt.Println(numbers, err)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3] &lt;nil&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A JSON array decodes straight into a Go slice. The error is <code>&lt;nil&gt;<\/code>, which means it worked.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output and Design Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\n\/\/ Base holds fields shared by many types.\ntype Base struct {\n    ID int `json:\"id\"`\n}\n\n\/\/ Employee embeds Base, so it gets the ID field too.\ntype Employee struct {\n    Base\n    Name string `json:\"name\"`\n}\n\nfunc main() {\n    employee := Employee{Base: Base{ID: 1}, Name: \"Dev\"}\n    data, _ := json.Marshal(employee)\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"id\":1,\"name\":\"Dev\"}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Fields of an embedded struct are flattened into the outer JSON object, just like promoted fields in Go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    data, _ := json.Marshal(\"&lt;b&gt;Go &amp; JSON&lt;\/b&gt;\")\n    fmt.Println(string(data))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\\u003cb\\u003eGo \\u0026 JSON\\u003c\/b\\u003e\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By default, Go escapes <code>&lt;<\/code>, <code>&gt;<\/code> and <code>&amp;<\/code> so the JSON is safe inside HTML. Use a <code>json.Encoder<\/code> with <code>SetEscapeHTML(false)<\/code> if you do not want this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. What is json.RawMessage used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It keeps part of the JSON as raw bytes, without decoding it. This is useful when one field&#8217;s shape depends on another field, such as a &#8220;type&#8221; field that tells you how to decode &#8220;payload&#8221; later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What is the difference between json.Unmarshal and json.NewDecoder?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Unmarshal<\/code> works on a byte slice you already have in memory. <code>NewDecoder<\/code> reads from an <code>io.Reader<\/code>, such as an HTTP body or a file, and can decode many values one after another.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. How do you send JSON in an HTTP response?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set the <code>Content-Type<\/code> header to <code>application\/json<\/code>, then call <code>json.NewEncoder(w).Encode(value)<\/code>. The encoder writes straight to the response, so you do not need a separate byte slice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task combines several Go JSON programs from above. Write a program that reads a JSON list of employees with name, department and salary, then prints the total salary for each department in sorted order. Hint: unmarshal into a slice of structs, add salaries into a <code>map[string]int<\/code>, then sort the keys.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/encoding\/json\">encoding\/json package documentation<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/json\">JSON and Go (Go blog)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Struct_types\">Go spec: struct types and tags<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go JSON programs use <code>json.Marshal<\/code> to write JSON and <code>json.Unmarshal<\/code> to read it.<\/li>\n\n\n<li>Struct tags control names, and <code>omitempty<\/code> and <code>-<\/code> hide fields.<\/li>\n\n\n<li>Unknown JSON goes into <code>map[string]any<\/code>, where numbers become <code>float64<\/code>.<\/li>\n\n\n<li><code>json.Decoder<\/code> streams JSON from files and HTTP bodies with little memory.<\/li>\n\n\n<li>Check errors with <code>errors.As<\/code> to give clients clear messages about bad JSON.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we start running code at the same time. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/\">Go goroutine programs with output<\/a>, with WaitGroup and the famous odd-even printing program. For the theory, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-json-encoding-decoding\/\">Go JSON guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go JSON programs turn Go structs into JSON text and turn JSON text back into Go values, which is how almost every Go API&hellip;<\/p>\n","protected":false},"author":1,"featured_media":815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,42,11,104],"swapz_series":[],"class_list":["post-818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-json","tag-go-lang","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go JSON Programs: Marshal, Unmarshal and Tags<\/title>\n<meta name=\"description\" content=\"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-json-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go JSON Programs: Marshal, Unmarshal and Tags\" \/>\n<meta property=\"og:description\" content=\"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-json-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-08T11:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go JSON Programs with Output: Marshal, Unmarshal and Struct Tags\",\"datePublished\":\"2026-10-08T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/\"},\"wordCount\":1699,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-json-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go JSON\",\"Go Lang\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/\",\"name\":\"Go JSON Programs: Marshal, Unmarshal and Tags\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-json-programs-featured.png\",\"datePublished\":\"2026-10-08T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:30+00:00\",\"description\":\"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-json-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-json-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go JSON Programs featured image: marshal, unmarshal and tags output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-json-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go JSON Programs with Output: Marshal, Unmarshal and Struct Tags\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go JSON Programs: Marshal, Unmarshal and Tags","description":"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-json-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go JSON Programs: Marshal, Unmarshal and Tags","og_description":"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-json-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-08T11:30:00+00:00","article_modified_time":"2026-11-28T19:59:30+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go JSON Programs with Output: Marshal, Unmarshal and Struct Tags","datePublished":"2026-10-08T11:30:00+00:00","dateModified":"2026-11-28T19:59:30+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/"},"wordCount":1699,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go JSON","Go Lang","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-json-programs\/","name":"Go JSON Programs: Marshal, Unmarshal and Tags","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png","datePublished":"2026-10-08T11:30:00+00:00","dateModified":"2026-11-28T19:59:30+00:00","description":"Go JSON programs with output: Marshal, Unmarshal, struct tags, omitempty, nested JSON, maps and streaming, plus the most asked JSON interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-json-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-json-programs-featured.png","width":2400,"height":1260,"caption":"Go JSON Programs featured image: marshal, unmarshal and tags output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-json-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go JSON Programs with Output: Marshal, Unmarshal and Struct Tags"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions\/893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/815"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=818"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}