Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
import (
"encoding/json"
"fmt"
"os"
"reflect"
)Parse A Simple JSON String as a Hashmap¶
byt := []byte(`{"name": "Dan", "company": "Apple"}`)
var dat map[string]stringjson.Unmarshal(byt, &dat)
datmap[company:Apple name:Dan]Parse A Complex JSON String as a Hashmap of Generic Type¶
byt := []byte(`{"num":6.13, "strs":["a", "b"]}`)
var dat map[string]interface{}json.Unmarshal(byt, &dat)
datmap[num:6.13 strs:[a b]]val, err := dat["num"]
val6.13reflect.TypeOf(val)float64strs = dat["strs"].([]interface{})
strs[a b]reflect.TypeOf(strs)[]interface {}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¶
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{6.13 [a b]}myObj.num6.13myObj.strs[a b]myObj.strs[0]aPartially Parse a Complex JSON String as a Struct¶
The standard library encoding/json supports parsing a JSON string partially.
byt := []byte(`{"num":6.13, "strs":["a", "b"]}`)
type MyStruct struct {
strs []string `json:"strs"`
}
var myObj MyStruct
json.Unmarshal(byt, &myObj)
myObj{[a b]}myObj.numrepl.go:1:1: type main.MyStruct has no field or method "num": myObj.nummyObj.strs[a b]myObj.strs[0]aNested Partially Parse a Complex JSON String as a Struct¶
The standard library encoding/json even supports nested partially parseing a JSON string.
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)releases[{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}]}]releases[0]{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}]}releases[0].TagNamev0.6.0releases[0].Assets[{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}]releases[0].Assets[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}releases[0].Assets[0].Nameicon-v0.6.0-darwin-amd64.tar.gzreleases[0].Assets[0].BrowserDownloadUrlhttps://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz