how to omit empty array

4,433 views
Skip to first unread message

Alexander Mills

unread,
Sep 2, 2020, 3:38:00 PM9/2/20
to golang-nuts
I have this code used to construct an ElasticSearch json query:

```
type Term = map[string]interface{}
type QueryString = map[string]interface{}
type Range = map[string]interface{}
type Match = map[string]string

type HasTermOrMatch = struct {
Term `json:"term,omitempty"`
QueryString `json:"query_string,omitempty"`
Match `json:"match,omitempty"`
Range `json:"range,omitempty"`
}

var boolField struct {
Must []HasTermOrMatch `json:"must"`
MustNot []HasTermOrMatch `json:"must_not"`
Filter []HasTermOrMatch `json:"filter"`
Should []HasTermOrMatch `json:"should"`
}
```

my question is - how do I omit an array on boolField if the array is empty?

I hear there might be an interface I can add something like this:


func (v Match) IsZero() bool {
   return len(v) < 1
}

func (v Term) IsZero() bool {
   return len(v) < 1
}

func (v Range) IsZero() bool {
   return len(v) < 1
}

func (v QueryString) IsZero() bool {
   return len(v) < 1
}


anyone know how to do this?
thanks
-alex




Nishanth Shanmugham

unread,
Sep 3, 2020, 6:35:05 AM9/3/20
to golang-nuts
> how do I omit an array on boolField if the array is empty?

Do you mean how to omit an empty array's field in the serialized JSON? If so, you can use "omitempty" like you did in the other type.

var boolField struct {
   Must []HasTermOrMatch `json:"must,omitempty"`
   MustNot []HasTermOrMatch `json:"must_not,omitempty"`
   Filter []HasTermOrMatch `json:"filter,omitempty"`
   Should []HasTermOrMatch `json:"should,omitempty"`
}

This should omit the slice field in the serialized JSON if the slice is nil or zero length.

Alexander Mills

unread,
Sep 3, 2020, 4:32:52 PM9/3/20
to golang-nuts
I was under the impression it wouldn't omit the array if it had zero length, hence my question.. 
i mean at some point we want an empty array in JSON, so I assume it's included by default

burak serdar

unread,
Sep 3, 2020, 6:42:13 PM9/3/20
to Alexander Mills, golang-nuts
On Thu, Sep 3, 2020 at 2:32 PM Alexander Mills
<alexande...@gmail.com> wrote:
>
> I was under the impression it wouldn't omit the array if it had zero length, hence my question..
> i mean at some point we want an empty array in JSON, so I assume it's included by default

If you define boolField as a type, then you can have a custom
marshaler that checks array sizes, something like this:

func (b boolField) MarshalJSON() ([]byte,error) {
data:=map[string]interface{}{}
if len(b.Must)>0 {
data["must"]=b.Must
}
...
return json.Marshal(data)
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7df1c875-ed2e-46c4-a6f3-2064485de805n%40googlegroups.com.

Nishanth Shanmugham

unread,
Sep 4, 2020, 12:25:57 AM9/4/20
to golang-nuts
> I was under the impression it wouldn't omit the array if it had zero length, hence my question.

Using omitempty omits the array/slice field in the encoding if it is zero length. Check out https://play.golang.org/p/B1LcICyEfeK.

Related encoding/json package doc for omitempty:

    The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

where "empty array, slice" means a slice that is nil or is of zero length.
Reply all
Reply to author
Forward
0 new messages