Unmarsahl Nested JSON in Go

It is undeniable that JSON (JavaScript Object Notation) has gained a lot of popularity in the last years. According to Stack Overflow Trends, more questions are asked about it than for any other data interchange format.

JSON will often be the best choice when working with data and writing JavaScript code, because it is a natural format of the language. However, there are implementations for many languages, Go being one of them.

Paraphrasing from the dictionary, decode is:

to convert something into a human readable form

Photo by Clément Falize on Unsplash

Go json package

The package json implements encoding and decoding of JSON. For this post we will focus on decoding, specifically nested JSON to a Go struct.

Unmarshal or decode?

Unmarshal is the function used for decoding. There is also a method called Decode in the json package, where its name is self explanatory of what it does. There is often a misconception between these two.

Which one should you use? As to every good question, the answer is, it depends. If you are reading values from an input stream, decode will be the right option for you.

We will be using the unmarshal function in this post. So let’s decode some JSON in Go!


Let’s say that we have a list of libraries in JSON. And we want this information organised in a struct like this:

type Libraries struct {
LibrariesList []struct {
Name string `json:"name"`
City string `json:"city"`
Country string `json:"country"`
Books []struct {
Title string `json:"title"`
Author string `json:"author"`
Availability bool `json:"availability"`
} `json:"books"`
} `json:"libraries"`

The json values are important since they make it possible for the Go package to map the values correctly. If the value is missing for a specific field, it will be initialised with a zero value.

The value of librariesInformation variable is printed out with its field names and the variable will look like this:

If you want to check the code you can do so here:

Hope you liked the explanation. Have you used unmarshal in Go before with a nested JSON? Do you think that it is tricky? Is there anything else that I can help you with? Let me know in the comments!

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Julia Álvarez de Eulate Sainz

Written by

Go Backend developer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store