Problem with nested Struct and JSON

package main

import (

type info struct {
	p person

type person struct {
	Name string

func main() {
	p1 := info{}
	data := `{"p":{"Name": "John"}}`

	json.Unmarshal([]byte(data) , &p1)


I am trying to save the name “John” in the Name field of p . But its not happening? Whats the error here ?

info.p needs to be exported:

type info struct {
	P person

Since json.Unmarshal is in a different package, it does not have access to unexpected fields.

Also, while it would not have helped in this case, always check returned errors. Before I realized that info.p needed to be exported, I added error checking for json.Unmarshal. Finding it did not return an error told me the problem was not with json.Unmarshal so I looked at what it was given and found the unexported info.p.

My method,, is quick and easy to implement.

Thanks bro …

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.