Home > Categories > Misc > How to Check JSON Key not-set or Null in Golang Unmarshalling

Home > How to Check JSON Key not-set or Null in Golang Unmarshalling

How to Check JSON Key not-set or Null in Golang Unmarshalling

Updated:

24 Jan 2021

Published:

24 Jan 2021

Golang project started as programming language for servers.

And all servers communicate with JSON. Still json unmarshalling of null or not-set key is not baked into golang.

We need to write custom unmarshal method to validate json.

On the positive side this gives us access to lower level code and we can do more than just checking for null key or not-set key.

To read rest of the article please Purchase Subscription

Before we get started I would recommend to develop project inside development docker container

So we no longer have the issue of "this works on my computer". And it will not mess with your existing workflow.

So checkout my blog post for detailed video explanation.

Video walk-through of article

Pro Tip: Enable Subtitle

Create new main.go file and add following code

There are few conditions that needs to be met before using custom unmarshal.

  1. JSON key must be known. e.g. in above example we knew the key was `views` if the key was set to `age` and we didn't know that then custom unmarshal won't work.
  2. JSON value type for key must be known. e.g. in above example we knew that value type for key `views` was `int` if the value type was `string` then unmarshal will fail.
  3. The default value set by unmarshal without custom method should be acceptable value. e.g. in above example if `views` is not-set or null then default value is `0`.

And if `0` is unacceptable value then we don't need custom method to validate json. For every `0` value we just assume its invalid.

But if `0` is acceptable value then we need to check if this is default value or actual value.

If above 3 conditions are met then you can use custom unmarshal.

You need to add custom type for every optional data type.

To explain this I have added single example OptionalInt.

You can set name of this type anything you want. Also properties name for this type anything that you want.

But you cannot set different name for UnmarshalJSON method. You MUST use this exact string.

This method MUST take in []byte and return error type.

You can set this functions variable name to anything you want.

Now because this is custom unmarshal function you can add further validation to the type at the end before returning nil.

So if you are accepting age between 0-100 and received value is greater than 100 then you can set isValid to false in the same function.

Conclusion

If you are using json decoding in golang then you must implement custom unmarshal method from above.

Because there is no convenience method available in golang. But on plus side you can further customize it to suit your needs.

Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.

If you like this article please consider purchasing paid