By tlehman

2013-03-16 16:58:12 8 Comments

I am using Go 1.0.3 on Mac OS X 10.8.2, and I am experimenting with the json package, trying to marshal a struct to json, but I keep getting an empty {} json object.

The err value is nil, so nothing is wrong according to the json.Marshal function, and the struct is correct. Why is this happening?

package main

import (

type Address struct {
  street string
  extended string
  city string
  state string
  zip string

type Name struct {
  first string
  middle string
  last string

type Person struct {
  name Name
  age int
  address Address
  phone string

func main() {
  myname := Name{"Alfred", "H", "Eigenface"}
  myaddr := Address{"42 Place Rd", "Unit 2i", "Placeton", "ST", "00921"}
  me := Person{myname, 24, myaddr, "000 555-0001"}

  b, err := json.Marshal(me)

  if err != nil {

  fmt.Println(string(b))    // err is nil, but b is empty, why?
  fmt.Println(me)           // me is as expected, full of data


@Lakhan Saiteja 2019-06-05 14:35:49

JSON library cannot view the fields in a struct unless they are public. In your case,

type Person struct {
  name Name
  age int
  address Address
  phone string

the fields name, age, address and phone are not public (start with a small letter). In golang, variables/functions are public, when they start with a capital letter. So for this to work, your struct needs to look something like this:

type Person struct {
  Name Name
  Age int
  Address Address
  Phone string

@lukad 2013-03-16 16:59:58

You have to make the fields that you want to marshal public. Like this:

type Address struct {
  Street string
  Extended string
  City string
  State string
  Zip string

err is nil because all the exported fields, in this case there are none, were marshalled correctly.

Working example:

Check out the docs

@tlehman 2013-03-16 17:21:49

That was it, I glossed over the line "Each exported struct field". Thank you.

@khue bui 2017-11-14 14:25:27

thank you for pointing out the "public".

@Camilo Crespo 2016-02-05 04:38:05

Note that you can also manipulate what the name of the fields in the generated JSON are by doing the following:

type Name struct {
  First string `json:"firstname"`
  Middle string `json:"middlename"`
  Last string `json:"lastname"` 

@tlehman 2016-02-05 17:21:59

I like this way of associating structs and json objects.

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

49 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 1880783 View
  • 6820 Score
  • 49 Answer
  • Tags:   json comments

22 Answered Questions

24 Answered Questions

[SOLVED] How can I pretty-print JSON using JavaScript?

53 Answered Questions

[SOLVED] How can I pretty-print JSON in a shell script?

40 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

9 Answered Questions

[SOLVED] Why can't Python parse this JSON data?

  • 2010-05-14 15:54:20
  • michele
  • 2568818 View
  • 1378 Score
  • 9 Answer
  • Tags:   python json parsing

34 Answered Questions

[SOLVED] What is the correct JSON content type?

6 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

25 Answered Questions

[SOLVED] Safely turning a JSON string into an object

  • 2008-09-05 00:12:01
  • Matt Sheppard
  • 1205502 View
  • 1275 Score
  • 25 Answer
  • Tags:   javascript json

Sponsored Content