Ben Chuanlong Du's Blog

It is never too late to learn.

Serialize and Deserialize JSON in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [2]:
import (
    "encoding/json"
    "fmt"
    "os"
    "reflect"
)

Parse A Simple JSON String as a Hashmap

In [9]:
byt := []byte(`{"name": "Dan", "company": "Apple"}`)
var dat map[string]string
In [10]:
json.Unmarshal(byt, &dat)
dat
Out[10]:
map[company:Apple name:Dan]

Parse A Complex JSON String as a Hashmap of Generic Type

In [11]:
byt := []byte(`{"num":6.13, "strs":["a", "b"]}`)
var dat map[string]interface{}
In [12]:
json.Unmarshal(byt, &dat)
dat
Out[12]:
map[num:6.13 strs:[a b]]
In [14]:
val, err := dat["num"]
val
Out[14]:
6.13
In [16]:
reflect.TypeOf(val)
Out[16]:
float64
In [27]:
strs = dat["strs"].([]interface{})
strs
Out[27]:
[a b]
In [28]:
reflect.TypeOf(strs)
Out[28]:
[]interface {}
In [30]:
strs[0].(string)
repl.go:1:6: invalid operation: strs[0] (type interface{} does not support indexing)

Parse a Complex JSON String as a Struct

In [38]:
byt := []byte(`{"num":6.13, "strs":["a", "b"]}`)
type MyStruct struct {
    num float64 `json:"num"`
    strs []string `json:"strs"`
}
var myObj MyStruct
json.Unmarshal(byt, &myObj)
myObj
Out[38]:
{6.13 [a b]}
In [39]:
myObj.num
Out[39]:
6.13
In [40]:
myObj.strs
Out[40]:
[a b]
In [41]:
myObj.strs[0]
Out[41]:
a

Partially Parse a Complex JSON String as a Struct

The standard library encoding/json supports parsing a JSON string partially.

In [42]:
byt := []byte(`{"num":6.13, "strs":["a", "b"]}`)
type MyStruct struct {
    strs []string `json:"strs"`
}
var myObj MyStruct
json.Unmarshal(byt, &myObj)
myObj
Out[42]:
{[a b]}
In [43]:
myObj.num
repl.go:1:1: type main.MyStruct has no field or method "num": myObj.num
In [44]:
myObj.strs
Out[44]:
[a b]
In [45]:
myObj.strs[0]
Out[45]:
a

Nested Partially Parse a Complex JSON String as a Struct

The standard library encoding/json even supports nested partially parseing a JSON string.

In [9]:
bytes := []byte(`[{
  "url": "https://api.github.com/repos/legendu-net/icon/releases/72107224",
  "assets_url": "https://api.github.com/repos/legendu-net/icon/releases/72107224/assets",
  "upload_url": "https://uploads.github.com/repos/legendu-net/icon/releases/72107224/assets{?name,label}",
  "html_url": "https://github.com/legendu-net/icon/releases/tag/v0.6.0",
  "id": 72107224,
  "author": {
    "login": "dclong",
    "id": 824507,
    "node_id": "MDQ6VXNlcjgyNDUwNw==",
    "avatar_url": "https://avatars.githubusercontent.com/u/824507?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/dclong",
    "html_url": "https://github.com/dclong",
    "followers_url": "https://api.github.com/users/dclong/followers",
    "following_url": "https://api.github.com/users/dclong/following{/other_user}",
    "gists_url": "https://api.github.com/users/dclong/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/dclong/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/dclong/subscriptions",
    "organizations_url": "https://api.github.com/users/dclong/orgs",
    "repos_url": "https://api.github.com/users/dclong/repos",
    "events_url": "https://api.github.com/users/dclong/events{/privacy}",
    "received_events_url": "https://api.github.com/users/dclong/received_events",
    "type": "User",
    "site_admin": false
  },
  "node_id": "RE_kwDOHkKAZc4ETETY",
  "tag_name": "v0.6.0",
  "target_commitish": "main",
  "name": "icon v0.6.0",
  "draft": false,
  "prerelease": false,
  "created_at": "2022-07-15T16:17:03Z",
  "published_at": "2022-07-15T16:17:30Z",
  "assets": [
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690405",
      "id": 71690405,
      "node_id": "RA_kwDOHkKAZc4EReil",
      "name": "icon-v0.6.0-darwin-amd64.tar.gz",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "application/gzip",
      "state": "uploaded",
      "size": 4991511,
      "download_count": 1,
      "created_at": "2022-07-15T16:18:13Z",
      "updated_at": "2022-07-15T16:18:13Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690408",
      "id": 71690408,
      "node_id": "RA_kwDOHkKAZc4EReio",
      "name": "icon-v0.6.0-darwin-amd64.tar.gz.md5",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 33,
      "download_count": 0,
      "created_at": "2022-07-15T16:18:14Z",
      "updated_at": "2022-07-15T16:18:14Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz.md5"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690400",
      "id": 71690400,
      "node_id": "RA_kwDOHkKAZc4EReig",
      "name": "icon-v0.6.0-darwin-arm64.tar.gz",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "application/gzip",
      "state": "uploaded",
      "size": 4811713,
      "download_count": 1,
      "created_at": "2022-07-15T16:18:11Z",
      "updated_at": "2022-07-15T16:18:11Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690403",
      "id": 71690403,
      "node_id": "RA_kwDOHkKAZc4EReij",
      "name": "icon-v0.6.0-darwin-arm64.tar.gz.md5",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 33,
      "download_count": 0,
      "created_at": "2022-07-15T16:18:12Z",
      "updated_at": "2022-07-15T16:18:12Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz.md5"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690355",
      "id": 71690355,
      "node_id": "RA_kwDOHkKAZc4ERehz",
      "name": "icon-v0.6.0-linux-amd64.tar.gz",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "application/gzip",
      "state": "uploaded",
      "size": 5186711,
      "download_count": 8,
      "created_at": "2022-07-15T16:18:05Z",
      "updated_at": "2022-07-15T16:18:05Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690359",
      "id": 71690359,
      "node_id": "RA_kwDOHkKAZc4EReh3",
      "name": "icon-v0.6.0-linux-amd64.tar.gz.md5",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 33,
      "download_count": 0,
      "created_at": "2022-07-15T16:18:06Z",
      "updated_at": "2022-07-15T16:18:06Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz.md5"
    },
    {
      "url": "https://api.github.com/repos/legendu-net/icon/releases/assets/71690414",
      "id": 71690414,
      "node_id": "RA_kwDOHkKAZc4EReiu",
      "name": "icon-v0.6.0-linux-arm64.tar.gz",
      "label": "",
      "uploader": {
        "login": "github-actions[bot]",
        "id": 41898282,
        "node_id": "MDM6Qm90NDE4OTgyODI=",
        "avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/github-actions%5Bbot%5D",
        "html_url": "https://github.com/apps/github-actions",
        "followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
        "following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
        "gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
        "organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
        "repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
        "events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
        "received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
        "type": "Bot",
        "site_admin": false
      },
      "content_type": "application/gzip",
      "state": "uploaded",
      "size": 4843937,
      "download_count": 0,
      "created_at": "2022-07-15T16:18:22Z",
      "updated_at": "2022-07-15T16:18:23Z",
      "browser_download_url": "https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-arm64.tar.gz"
    }
  ],
  "tarball_url": "https://api.github.com/repos/legendu-net/icon/tarball/v0.6.0",
  "zipball_url": "https://api.github.com/repos/legendu-net/icon/zipball/v0.6.0",
  "body": ""
}]`)
type AssetInfo struct {
    Name string `json:"name"`
    BrowserDownloadUrl string `json:"browser_download_url"`
}
type ReleaseInfo struct {
    TagName string `json:"tag_name"`
    Assets []AssetInfo `json:"assets"`
}
var releases []ReleaseInfo
json.Unmarshal(bytes, &releases)
In [10]:
releases
Out[10]:
[{v0.6.0 [{icon-v0.6.0-darwin-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz} {icon-v0.6.0-darwin-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz.md5} {icon-v0.6.0-darwin-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz} {icon-v0.6.0-darwin-arm64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz.md5} {icon-v0.6.0-linux-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz} {icon-v0.6.0-linux-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz.md5} {icon-v0.6.0-linux-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-arm64.tar.gz}]}]
In [11]:
releases[0]
Out[11]:
{v0.6.0 [{icon-v0.6.0-darwin-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz} {icon-v0.6.0-darwin-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz.md5} {icon-v0.6.0-darwin-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz} {icon-v0.6.0-darwin-arm64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz.md5} {icon-v0.6.0-linux-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz} {icon-v0.6.0-linux-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz.md5} {icon-v0.6.0-linux-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-arm64.tar.gz}]}
In [12]:
releases[0].TagName
Out[12]:
v0.6.0
In [14]:
releases[0].Assets
Out[14]:
[{icon-v0.6.0-darwin-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz} {icon-v0.6.0-darwin-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz.md5} {icon-v0.6.0-darwin-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz} {icon-v0.6.0-darwin-arm64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-arm64.tar.gz.md5} {icon-v0.6.0-linux-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz} {icon-v0.6.0-linux-amd64.tar.gz.md5 https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-amd64.tar.gz.md5} {icon-v0.6.0-linux-arm64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-linux-arm64.tar.gz}]
In [15]:
releases[0].Assets[0]
Out[15]:
{icon-v0.6.0-darwin-amd64.tar.gz https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz}
In [16]:
releases[0].Assets[0].Name
Out[16]:
icon-v0.6.0-darwin-amd64.tar.gz
In [17]:
releases[0].Assets[0].BrowserDownloadUrl
Out[17]:
https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz
In [ ]:
 

Comments