How can i unmarshal json to the struct?

Hello everyone.

Here is my struct

type Form struct {
	Key string `json:"key"`
	Label string `json:"label"`
	Type string `json:"type"`
	NotNull bool `json:"notNull"`
	Enums []Enum `json:"enums"`
	Custom *Form `json:"custom"`
} 

here is the JSON

{
        "key": "schoolInfo",
        "placeHolder": "",
        "label": "school Info",
        "type": "Custom",
        "notNull": false,
        "enums": [],
        "custom": [
          {
            "key": "departmentId",
            "placeHolder": "",
            "label": "Department",
            "type": "Id",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          },
          {
            "key": "studentId",
            "placeHolder": "",
            "label": "Student Id",
            "type": "String",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          },
          {
            "key": "currentDegreeType",
            "placeHolder": "",
            "label": "DegreeType",
            "type": "Enum",
            "notNull": false,
            "enums": [
              {
                "label": "BACHELOR",
                "value": "BACHELOR"
              },
              {
                "label": "MASTER",
                "value": "MASTER"
              },
              {
                "label": "DOCTOR",
                "value": "DOCTOR"
              },
              {
                "label": "SUCCESSIVE_POSTGRADUATE_AND_DOCTORAL",
                "value": "SUCCESSIVE_POSTGRADUATE_AND_DOCTORAL"
              },
              {
                "label": "FOREIGN_STUDENT",
                "value": "FOREIGN_STUDENT"
              },
              {
                "label": "COLLEGE_DEGREE",
                "value": "COLLEGE_DEGREE"
              }
            ],
            "custom": [],
            "tags": []
          },
          {
            "key": "majorId",
            "placeHolder": "",
            "label": "Major",
            "type": "Id",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          },
          {
            "key": "grade",
            "placeHolder": "",
            "label": "Grade",
            "type": "String",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          },
          {
            "key": "estimatedGraduationTime",
            "placeHolder": "",
            "label": "",
            "type": "Date",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          },
          {
            "key": "departmentGrade",
            "placeHolder": "",
            "label": "",
            "type": "String",
            "notNull": false,
            "enums": [],
            "custom": [],
            "tags": []
          }
        ],
        "tags": []
      }

Here is the code I was unmarshaling the JSON to the struct.

var form form_definition.Form
	err := json.Unmarshal([]byte(aJson), &form)
	if err != nil {
		logrus.Error("Found err!")
		println(err)
	}else {
		println(form.Key)
	}

But I got an error. The error looks so strange for me
Here is the error

time="2019-08-23T17:39:29+08:00" level=error msg="Found err!"
(0x531a80,0xc0000960a0)

When I modify the struct to below everything is fine.

type Form struct {
	Key string `json:"key"`
	Label string `json:"label"`
	Type string `json:"type"`
	NotNull bool `json:"notNull"`
	Enums []Enum `json:"enums"`
	//Custom *Form `json:"custom"`
}

My question is how can I convert the JSON to the struct?
Thank you all.

1 Like

Sorry. The type of the custom field of the JSON is an array. But in my struct, the custom is not an array. This is the reason why I am got the error

3 Likes

Welcome to the forums!

Sounds like you figure it out. Any other questions about json marshaling?

1 Like

Thank you. I don`t have other question about json marshaling. The way of marshalling json in go is so easy. I love golang. I just remembered there is a question about marshalling json. How can I know an json type ? I need to know the json is an array or is an object. Do you know how to get the type of a json ? Than you again.

1 Like

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