Now Reading: Go JSON Omitempty Usage (JSON Omitempty)

Loading
svg
Open

Go JSON Omitempty Usage (JSON Omitempty)

January 21, 20245 min read

JSON is probably one of the most popular data interchange formats of the modern era. It is very simple yet powerful and versatile for powering large-scale applications and API endpoints.

So there is no doubt that working with JSON is very common in languages ​​like Go. However, many times, when serializing structures into JSON objects and vice versa, we need to control the fields we want to include in the output.

This is where the “omitempty” attribute or tag comes into play. This allows us to tell the compiler not to include any fields that are empty or have a value of zero.

In this tutorial, we will explore the working of this tag and how it can help in serializing and deserializing JSON data in Go language.

What is Omitempty?

omitempty is a structure field tag that we can use in conjunction with the encoding/JSON package to control the serialization of Go structure fields in JSON objects.

When the JSON encoder encounters the “omitempty” field tag on a structure, it omits the field from the JSON output if it contains an empty value or null.

This is especially useful when we want to create more concise JSON representations or filter out unnecessary data.

Use omitempty tag in golang

Let's learn how to use “omitempty” tag in Go structure.

We can start by defining a structure and adding tags as shown in the following syntax:

Field Type 'json:'field,omitblank''

For example:

Type car structure ,
Sample string 'json:”model”'
Year int here 'json:”year,omitblank”'
Benefit int here 'json:”mileage,omitempty”'
,

In this case, we define a structure called “Car” with three main fields. “year” and “mileage” include an “omitempty” tag that allows the encoder to discard them as valid JSON in the output if they contain 0 or the empty string.

Encode a structure to JSON

To encode an instance of the struct into JSON with omitempty handling, we can use the “json.Marshal” function from the encoding/JSON package.

package main
Import ,

“encoding/json”

“fmt”
,

Type car structure ,
Sample string 'json:”model”'
Year int here 'json:”year,omitblank”'
Benefit int here 'json:”mileage,omitempty”'
,
main work,, ,
car :, car,
Sample: “Audi”,
Year: 0,
Mileage: 200,
,
jsonData, Mistake:, json.Marshall,car,
If to make a mistake , Zero ,
FMT.println,“Mistake:”, to make a mistake,
return
,
FMT.println,string,jsonData,,

,

In this case, since the value of the “Year” field is 0, it is removed from the JSON output as shown in the following example:

,“Sample”,“Audi”,“Mileage”,200

Decode JSON data into a structure

To decode JSON data into Go structures with omitempty handling, we can use the “json.Unmarshal” function from the encoding/JSON package.

An example is as follows:

package main
Import ,
“encoding/json”
“fmt”
,
Type car structure ,
Sample string 'json:”model”'
Year int here 'json:”year,omitblank”'
Benefit int here 'json:”mileage,omitempty”'
,
main work,, ,
jsonStr:, '{“model”:”Audi”,”year”:0,”mileage”:200}'
there is car
Mistake:, json.Unmarshal,[]byte,jsonStr,, &car,
If to make a mistake , Zero ,
FMT.println,“Mistake:”, to make a mistake,
return
,
FMT.printf,“Model: %s, Year: %d, Mileage: %d\n,, car.Sample, car.Year, car.Benefit,
,

In this example, if the JSON data does not contain a “year” field, the “year” field in the “car” structure will be set to an empty string. This allows us to handle optional fields beautifully.

Delete empty slices and maps

We can also apply empty or null value filtering to a slice or map of a given structure as shown in the following example:

package main

Import ,
“encoding/json”
“fmt”
,

Type car structure ,
Sample string 'json:”model”'
Colour string 'json:”color”'
features []string 'json:'attributes,omitblank''
Property Map[string]string 'json:”attributes,omitblank”'
,

main work,, ,
car :, car,
Sample: “Toyota Camry”,
Colour: “silver”,
features: []string,“GPS”, “leather seats”,,
Attribute: make,Map[string]string,,
,

jsonData, ,, json.Marshall,car,
FMT.println,string,jsonData,,
,

The resulting string is as follows:

,“Sample”,“Toyota Camry”,“Colour”,“silver”,“features”,[“GPS”,“Leather Seats”],

You'll notice that the “omitempty” tag is applied to the attribute map which contains empty values ​​and therefore is not included in the resulting JSON string.

conclusion

In this tutorial, we learned all about the “omitempty” tag in Go Structures, allowing us to exclude any fields containing an empty string with a null value in the resulting JSON string and vice versa.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg