leogazio
(Leonardo Gazio)
1
Hey there, why if I use type sql.NullString and when serialize it to json I have the following result?
"SerieSatelital":{"String":"","Valid":false}
Instead of:
"SerieSatelital":null
I’m doing it that way;
err := rows.Scan(&v.IdVeiculoGrupo, &v.Identificacao, &v.SerieSatelital)
.....................
...............
.........
json.NewEncoder(w).Encode(&Veiculos)
What should I do to get the correct null format when serialized to json?
nathankerr
(nathankerr)
2
Hey there, why if I use type sql.NullString and when serialize it to json I have the following result?
sql.NullString
is a struct. The output you see is the default way structs are encoded/marshaled to JSON (using reflection).
What should I do to get the correct null format when serialized to json?
Two ways come to mind:
- Use
*string
instead of sql.NullString
- Embed
sql.NullString
in a type that implements json.Marshaler
, then use that type instead
Go Playground - The Go Programming Language shows how both these methods work.
I tend to use pointers to represent nullable values.
2 Likes
system
(system)
Closed
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.