Skip to content

How to fix Viper unmarshal error for nested structs

workplace with laptop and ceramic vase with basket

In times when you’re trying to unmarshal a config file using viper, check whether your config file has nested yaml structures.

  api_key: some_api_key
      api_key: some_api_key

As shown in the example above, there are multiple layers of structs under providers (i.e. payment contains dwolla). The different with service is that its fields are set only in one level.

If your config file is a single level struct, then there won’t be any problems using the yaml Golang structural flag. However, if your config file uses multiple-level structs as in the case of providers, then you need to use mapstructure instead of yaml when defining your structs.

See this stack overflow resource for more information.

I’ll build out a small reproducible repository to demonstrate this later.

New to

Hi there reader! My name is Darren and I write about technology, learning, and gaming for mid to senior level software engineers. I love learning and finding lessons (and mistakes) in things that I do.

Sharing what we learn enables us to encourage, uplift, and empower the next set of engineering problem solvers. Writing them down also reminds me to seal the lesson in and not forget them!

I have a series of mental models that helps you think and strategize for problems whether in engineering or in your personal life. I also share short stories about reflections of my life, studying the similarities of playing competitively both in games and in performance at work.

Here are some trending things I’ve been talking about lately:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.