{"id":990,"date":"2012-10-26T19:25:01","date_gmt":"2012-10-26T19:25:01","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=990"},"modified":"2012-10-26T19:25:01","modified_gmt":"2012-10-26T19:25:01","slug":"json-decoding-in-go-golang","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/10\/json-decoding-in-go-golang\/","title":{"rendered":"JSON Decoding in Go (golang)"},"content":{"rendered":"<p>A quick example of JSON decoding (or unmarshaling) in Go. This is basically magic, it&#8217;s totally amazing I love it.<\/p>\n<p>It&#8217;s possible to decode the JSON into generic maps, but if you know the structure you can get the JSON to populate native go structures automatically. You need to be mindful of types, go wont for example convert an int to a string, it&#8217;ll just ignore it.<\/p>\n<p>This means if you have a JSON object that contains string:int key values and string:string key values and you try to populate a golang string map from it, you&#8217;ll lose all you string:int pairs. You are better off creating a struct to represent it as seen below.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npackage main\r\n\r\nimport (\r\n\t&quot;encoding\/json&quot;\r\n\t&quot;fmt&quot;\r\n)\r\n\r\ntype LogEntry struct {\r\n\tTime string\r\n\tCpm\t\tfloat32\r\n\tDuration\tint32\r\n\tAccel_x_start\tint32\r\n\tAccel_y_start\tint32\r\n\tAccel_z_start\tint32\r\n\tAccel_x_end\tint32\r\n\tAccel_y_end\tint32\r\n\tAccel_z_end\tint32\r\n}\r\n\r\ntype Log struct {\r\n  Log_size \t\t\tint\r\n\tOnyx_version \tstring\r\n\tUTC_offset\t\tstring\r\n\tLog_data\t\t\t&#x5B;] LogEntry\r\n\r\n}\r\n\r\nfunc main() {\r\n\r\n\tb := &#x5B;]byte(`{&quot;log_size&quot;:61,&quot;onyx_version&quot;:&quot;pre11&quot;,&quot;UTC_offset&quot;:&quot;undef&quot;,&quot;log_data&quot;:&#x5B;{&quot;time&quot;:&quot;2012-10-23T22:23:54Z&quot;,&quot;cpm&quot;:46,&quot;duration&quot;:30,&quot;accel_x_start&quot;:-4,&quot;accel_y_start&quot;:-97,&quot;accel_z_start&quot;:-3,&quot;accel_x_end&quot;:-1,&quot;accel_y_end&quot;:-98,&quot;accel_z_end&quot;:-2}]}`)\t\r\n\r\n\tvar l Log\r\n\tjson.Unmarshal(b, &amp;l)\r\n\r\n  fmt.Printf(&quot;logsize: %d onyxversion: %s data1: %s\\n&quot;,l.Log_size,l.Onyx_version,l.Log_data&#x5B;0]);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A quick example of JSON decoding (or unmarshaling) in Go. This is basically magic, it&#8217;s totally amazing I love it. It&#8217;s possible to decode the JSON into generic maps, but if you know the structure you can get the JSON to populate native go structures automatically. You need to be mindful of types, go wont [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-fY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=990"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/990\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/990\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}