{"id":153,"date":"2022-05-13T03:25:23","date_gmt":"2022-05-13T03:25:23","guid":{"rendered":"https:\/\/im8bit.com\/?p=153"},"modified":"2022-05-13T03:27:13","modified_gmt":"2022-05-13T03:27:13","slug":"golang-marshalling-struct-into-json","status":"publish","type":"post","link":"https:\/\/im8bit.com\/?p=153","title":{"rendered":"golang: Marshalling Struct into JSON"},"content":{"rendered":"\n<p>You might be here because an AWS error when trying to save to DynamoDB similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ValidationException: One or more parameter values were invalid: Missing the key id in the item\nstatus code: 400, request id: 29URI3A5PNH12CFDGTFA261O11VV4KQNSO5AEMVJF66Q9ASUBBNF<\/code><\/pre>\n\n\n\n<p>or just trying to figure out why your json structure doesn&#8217;t have certain fields after marshalling.<\/p>\n\n\n\n<p>In go if you have the following structure and are trying to fill it and marshalling like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type DBItem struct {\n\tid        int     `json:\"id\"`\n\tdate      int     `json:\"date\"`\n\tetype     string  `json:\"etype\"`\n\tother     string  `json:\"other\"`\n}\n\nitem := DBItem {\n\tid:    1200,\n\tdate:  9823123123,\n\tetype: \"type 1\",\n\tother: \"Other Value\",\n}\n\nav, err := dynamodbattribute.MarshalMap(item)\nfmt.Printf(\"after: %+v\", av)\n\njson, _ := json.MarshalIndent(response, \"\", \"  \")\nfmt.Printf(\"after: %s\", json)<\/code><\/pre>\n\n\n\n<p>You are going to get the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>after: map&#91;]\nafter: {}<\/code><\/pre>\n\n\n\n<p>An empty structure. This is happening because in golang whenever the field is lowercase means it will not be exported, in order to fix this you just need to change the field names in the structure to uppercase, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type DBItem struct {\n\tId        int     `json:\"id\"`\n\tDate      int     `json:\"date\"`\n\tEtype     string  `json:\"etype\"`\n\tOther     string  `json:\"other\"`\n}\n\nitem := DBItem {\n\tId:    1200,\n\tDate:  9823123123,\n\tEtype: \"type 1\"\n}\n\nav, err := dynamodbattribute.MarshalMap(item)\nfmt.Printf(\"after: %+v\", av)\n\njson, _ := json.MarshalIndent(response, \"\", \"  \")\nfmt.Printf(\"after: %s\", json)<\/code><\/pre>\n\n\n\n<p>This will get you:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>after: map&#91;other:{\n  NULL: true\n} date:{\n  N: \"0\"\n} etype:{\n  S: \"type 1\"\n} id:{\n  N: \"1\"\n}]\n\nafter: {\n  \"id\": 1,\n  \"date\": 0,\n  \"etype\": \"Expense Type\",\n  \"other\": \"\"\n}<\/code><\/pre>\n\n\n\n<p>Done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might be here because an AWS error when trying to save to DynamoDB similar to this: or just trying to figure out why your json structure doesn&#8217;t have certain fields after marshalling. In go if you have the following structure and are trying to fill it and marshalling like this: You are going to &#8230; <a class=\"read-more\" href=\"https:\/\/im8bit.com\/?p=153\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/im8bit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":2,"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/im8bit.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/im8bit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/im8bit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/im8bit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}