Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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!

Parse A Simple JSON String as a Hashmap

map[company:Apple name:Dan]

Parse A Complex JSON String as a Hashmap of Generic Type

map[num:6.13 strs:[a b]]
6.13
float64
[a b]
[]interface {}
repl.go:1:6: invalid operation: strs[0] (type interface{} does not support indexing)

Parse a Complex JSON String as a Struct

{6.13 [a b]}
6.13
[a b]
a

Partially Parse a Complex JSON String as a Struct

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

{[a b]}
repl.go:1:1: type main.MyStruct has no field or method "num": myObj.num
[a b]
a

Nested Partially Parse a Complex JSON String as a Struct

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

[{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}]}]
{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}]}
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}]
{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
https://github.com/legendu-net/icon/releases/download/v0.6.0/icon-v0.6.0-darwin-amd64.tar.gz