
Golang's json omitempty handling is a powerful feature that allows you to exclude missing and optional fields from JSON output.
By using the omitempty tag, you can control which fields are included in the JSON output, which is particularly useful when dealing with optional fields.
In the example of the struct Person, using the omitempty tag on the Address field means it will be excluded from the JSON output if it's empty.
This can save a lot of unnecessary data being sent over the wire, which is especially important in applications where bandwidth is limited.
A fresh viewpoint: How to Update a Github Using Golang
JSON Tags in Go
The omitempty tag is a game-changer for simplifying your JSON output in Go.
The omitempty tag is added to the field tag to indicate that the field should be omitted from the JSON output if its value is the zero value of the type. For most types, the zero value is either nil, false, 0, or an empty string.
See what others are reading: Replace Value and Create a Pr Using Golang
Using a pointer to a struct with the omitempty tag is useful when you have a struct that contains multiple fields and you want to represent it in JSON, but you want to omit the entire struct if any of its child fields are empty.
You can use the omitempty tag to conditionally omit fields from the JSON output, making it easier to work with your data in your application. This is especially useful when you have nested structs, like an Address field with Street, City, and State fields.
The omitempty tag can be used on individual fields, but it's more powerful when used on a pointer to a struct. This allows you to omit the entire struct if any of its child fields are empty, rather than just individual fields.
For your interest: Golang Copy Struct
Handling Missing Values
Go's json package will assign values only to fields found in the JSON, leaving other fields with their Go zero values, unless you specify otherwise.
Related reading: Golang Go
If you want to handle missing values differently, you can use the omitempty tag. This tag tells Go to omit fields with zero values from the JSON output.
However, if you need to differentiate between zero and null values, you can use the approach of treating 0 as null. This can be useful when you want to distinguish between having zero and not having information.
Serialize Missing Values
Serialize Missing Values can be tricky in Go, but don't worry, I've got you covered. In Go, types like uint64 or string can't be nil, but their pointers can, so we must hold a pointer reference in our entity.
You can use the default approach to set default values for optional/nonexistent fields, but this might not be ideal if you need to differentiate between 0 and null values.
For example, if you have an optional field called childrenCount, which holds the number of kids each employee has, you might want to behave 0 as if it's null, but you can't do that if you need to show 0 as a valid value for a specific case.
Readers also liked: T Golang
Go has an omitempty tag that helps us to not serialize empty fields, which is great if it's okay for your business requirement. However, if you need to serialize nonexistent fields as null, there is a library called guregu/null that is widely used by the Go community.
This library can be a lifesaver if you need to use null values explicitly, but in most cases, omitting empty values is enough for the client to see it as null.
Here's an interesting read: Golang vs Go
Serialize Empty Fields, Not Nonexistent Ones
If you want to serialize empty fields, but not nonexistent ones, Go's omitempty tag is your friend. It helps prevent empty fields from being serialized, which is perfect for fields like ownedCars that have a null value.
For example, if you have a struct with an ownedCars field that's null, using the omitempty tag will prevent it from being serialized. But if you have a field like childrenCount that's optional and has a value of 0, the omitempty tag will also omit it, even though it's a valid value.

Here's a summary of what happens when using omitempty:
So, if you want to serialize empty fields, but not nonexistent ones, make sure to use the omitempty tag and set it to null, 0, or an empty string. This way, you'll only serialize fields that have a valid value.
Default Values and Nil
In Go, you can set default values for optional fields, but this can sometimes be tricky. You don't want a zero value to be treated as the empty value for a field.
For example, if you have a struct describing a restaurant, you might want to set the number of customers to 0, but not omit the field altogether. This is where using pointers comes in handy. By using a pointer to an int, you can differentiate between a zero value and a nil pointer.
Here are the conditions under which a value is considered empty in Go:
- false for boolean types
- 0 for numeric types
- "" for strings
- nil for pointers, interfaces, maps, slices, and channels
- An array, slice, or map of length zero
Zero vs Omit

In Go, a value is deemed empty if it is false for boolean types, 0 for numeric types, "" for strings, nil for pointers, interfaces, maps, slices, and channels, or an array, slice, or map of length zero.
Using omitempty can lead to situations where fields with zero values are omitted, which might not always be desirable. For example, if you want to include fields with zero values, avoid using omitempty for those fields.
You might think it's okay to omit empty fields, but what if you need to include fields with zero values? In that case, you can avoid using omitempty altogether.
Here's a table to summarize the difference between zero values and omitted fields:
By understanding the difference between zero values and omitted fields, you can make informed decisions about how to handle empty fields in your Go code.
Default Values and Nil
Default values and nil are two concepts that can be tricky to work with in Go. A struct describing a restaurant with a default value of 0 for the number of customers is not the same as a nil value.
In Go, types such as uint64 or string cannot be nil, but their pointers can. This means we must hold a pointer reference in our entity to differentiate between a default value and a nil value.
If we have an optional field called childrenCount, which holds the number of kids each employee has, we wouldn't want to show 0 for both fields, because having 0 and not having information are two different things.
A value is deemed empty in Go if it is false for boolean types, 0 for numeric types, "" for strings, nil for pointers, interfaces, maps, slices, and channels, or an array, slice, or map of length zero.
Here's a summary of when a value is considered empty in Go:
- false for boolean types
- 0 for numeric types
- "" for strings
- nil for pointers, interfaces, maps, slices, and channels
- An array, slice, or map of length zero
To set default values, we can use a pointer field, which allows us to differentiate between a default value and a nil value. This approach is useful when we want to convey information about a field, such as the number of customers in a restaurant, without omitting it altogether.
Nested and Complex Data
The omitempty tag doesn't automatically omit nested structs, even if all their fields are empty. For instance, if Size is an empty Dimensions struct, it will still appear in the JSON output.
To omit nested structs, you can define them as pointers, like this: `Size *Dimensions`. Now, if Size is nil, it will be omitted.
When dealing with complex data, it's essential to consider how the JSON configuration can be missing some fields, and how to handle unknown fields. The json package is permissive by default and will ignore unknown fields, unless you specify the DisallowUnknownFields option. This can be done by passing the option to the JSON decoder.
Here's a summary of how to handle unknown fields:
Nested Structs
Nested Structs can be tricky to work with, especially when trying to omit empty fields.
The omitempty tag doesn't automatically omit nested structs, even if all their fields are empty.
If you have a nested struct with empty fields, it will still appear in the JSON output. For instance, if Size is an empty Dimensions struct, it will still be present.
To omit nested structs when they have zero values, you can define them as pointers. This way, if Size is nil, it will be omitted from the JSON output.
This approach can be a lifesaver when dealing with complex data structures.
Consider reading: Create a List of Structs Golang
Partial Unmarshaling and Unknown Fields
Partial unmarshaling is a common issue when working with complex data. If a JSON configuration file is missing some fields, Go's json package will assign default values to those fields, which can be undesirable.
For example, if the JSON configuration doesn't have the level field at all, the Options struct unmarshaled from it would have 0 for Level. This behavior is known as "omitempty" and can be configured by using the omitempty tag on the struct fields.
The json package is very permissive by default and will ignore unknown fields, which can be both an advantage and a disadvantage. This means that if the input JSON has a field that your struct doesn't have, it will be ignored and won't cause an error.
However, in some cases, you may want to report an error when encountering unknown fields. To achieve this, you can use the DisallowUnknownFields option with a JSON decoder. This will cause json.Unmarshal to return an error when it encounters an unknown field.
For more insights, see: Golang Create Error
Here's a summary of the options:
In general, it's a good idea to carefully consider the trade-offs between permissiveness and strictness when working with complex data. By understanding the behavior of the json package and using the right configuration options, you can write more robust and reliable code.
Special Cases and Considerations
In some cases, Go's json package will assign default values to missing fields, but you may want to handle special cases like extra fields in the JSON configuration. The json package is very permissive by default and will ignore unknown fields.
You can configure the json package to report an error when encountering unknown fields by using the DisallowUnknownFields option. This can be useful when you want to ensure that your Go struct matches the JSON configuration exactly.
Here are some scenarios to consider when using the omitempty tag:
- Use omitempty sparingly, as it can make a difference in how your application consumes the JSON objects.
- Make sure that Go's definition of "empty" matches your application's definition.
Ultimately, the choice of how to handle special cases depends on your specific use case and requirements.
Handling Time Fields

Handling time fields requires some special care.
The time.Time type is a struct, which means its behavior is different from what you might expect.
To omit time.Time fields when they are zero, define them as pointers. This is because the zero value of a struct field is not considered empty by omitempty.
If a Timestamp field is nil, it will be omitted from the JSON output, making it a convenient way to handle missing data.
When to Use
Use the omitempty tag sparingly, as it should only be used when the application consuming the JSON objects generated by your Go application differentiates between undefined keys and zero value keys.
If your application doesn't care about the difference, then there's no need to use omitempty in the first place. This is because Go's definition of "empty" - including zero value keys, empty strings, nil pointers, and zero-element slices - might not match your application's definition of "empty".
When deciding to use omitempty, make sure to follow Go's definition of "empty" to avoid any potential issues. This includes using the tag on fields that have a default value, such as an int with a default value of 0.
For more insights, see: Golang Applications
Best Practices and Tips
When working with Golang's json.Marshal function, it's essential to understand the impact of theomitempty tag. This tag tells the JSON encoder to omit the field if it's empty.
Theomitempty tag can be applied to any struct field, including strings, integers, and even slices. For example, in the article, we saw how theomitempty tag was used to omit an empty string.
To use theomitempty tag effectively, make sure to apply it to all fields that can potentially be empty. This includes fields that might be initialized to zero or an empty string by default.
If you forget to apply theomitempty tag, you might end up with JSON output that includes unnecessary null values. This can be especially problematic when working with large datasets.
In the example of the User struct, theomitempty tag was used to omit the empty string from the JSON output. This helps keep the JSON output clean and concise.
When working with nested structs, make sure to apply theomitempty tag to all fields, not just the top-level fields. This ensures that empty fields are omitted throughout the entire JSON output.
Theomitempty tag can also be used to omit fields that have a default value. For example, if a field is initialized to a default value, theomitempty tag can be used to omit it from the JSON output.
Setting and Handling Defaults
Go's default values are assigned to fields because types like uint64 or string can't be nil, but their pointers can.
Missing fields in the JSON representation will be decoded to zero values in Go, which is fine if your options' default values are also their zero values.
However, this isn't always the case, and you might need to set a default value like 10 for a field called Power, not 0.
To solve this, you can set the default values first and then let json.Unmarshal override fields as needed.
For your interest: Golang Set Env Variable
Alternatively, you can cleverly hide this logic in a custom UnmarshalJSON method for your struct.
This approach is simple and clean, but it has downsides like strongly tying default values with parsing logic and only working in simple cases.
If your struct has a slice or map of other structs, you can't populate defaults this way and need to write a custom unmarshal method for each nested struct.
Suggestion: Simple Http Server Golang Github
Featured Images: pexels.com


