Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning null to JSON fields instead of empty strings

Tags:

go

Since empty string is the zero/default value for Go string, I decided to define all such fields as interface{} instead. for example

type student struct {     FirstName  interface{} `json:"first_name"`     MiddleName interface{} `json:"middle_name"`     LastName   interface{} `json:"last_name"` } 

The application I am sending my data expect a null instead of an empty string if value is not available for that specific field.

Is this the correct approach or can someone please point me to something better than this.

like image 372
Prashant Avatar asked Jun 25 '15 11:06

Prashant


People also ask

Can a JSON field be null?

Null valuesJSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

Is empty string null in JSON?

Strings, Booleans and integers do not have an 'empty' form, so there it is okay to use null values.

Does JSON allow empty string?

As an empty string is not valid JSON it would be incorrect for JSON. parse('') to return null because "null" is valid JSON.

Should JSON include null values?

You should definitely include it if there is any need to distinguish between null and undefined since those have two different meanings in Javascript. You can think of null as meaning the property is unknown or meaningless, and undefined as meaning the property doesn't exist.


2 Answers

In json package documentation :

Pointer values encode as the value pointed to. A nil pointer encodes as the null JSON object.

So you can store a pointer to a string which will be encoded as a string if not nil and will be encoded as "null" if nil

type student struct {   FirstName  *string `json:"first_name"`   MiddleName *string `json:"middle_name"`   LastName   *string `json:"last_name"` } 
like image 63
Asdine Avatar answered Oct 13 '22 12:10

Asdine


Another way actually is a workaround with using the MarhshalJSON and UnmarshalJSON interface method offered by the json lib of golang. The code is as below:

type MyType string type MyStruct struct {     A MyType `json:"my_type"` }  func (c MyType) MarshalJSON() ([]byte, error) {     var buf bytes.Buffer     if len(string(c)) == 0 {         buf.WriteString(`null`)     } else {         buf.WriteString(`"` + string(c) + `"`)   // add double quation mark as json format required     }     return buf.Bytes(), nil }  func (c *MyType)UnmarshalJSON(in []byte) error {     str := string(in)     if str == `null` {         *c = ""         return nil     }     res := MyType(str)     if len(res) >= 2 {         res = res[1:len(res)-1]     // remove the wrapped qutation     }     *c = res     return nil } 

then when using json.Marshal, the MyType value will be marshaled as null.

like image 35
Chao Avatar answered Oct 13 '22 12:10

Chao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!