How do I tell sqlc to omit a field in the json if it's empty #1087
Replies: 3 comments 4 replies
-
There is currently not a way a to do this. What you can do is use a custom struct and override the output of the query to return that struct. |
Beta Was this translation helpful? Give feedback.
-
|
In the same vein, could sql add support for golang json struct flag type User struct {
Id int64 `json:"id,string"`
} |
Beta Was this translation helpful? Give feedback.
-
|
I have opened a PR for adding support for |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I have a table
After I run
sqlc generatethe following model will be generated:I would like to omit the description if it's empty (as it's a nullable column).
Now, I can use column override and use
stringtype instead ofsql.NullString. So now the generated type will beThe problem with this is that if Description is
nil, it won't be omitted from thejsonresponse. So the response would be something like this:{ "id": "c0507d40-de51-451f-b84e-c2f06af7710f", "title": "This is a todo", "description": null }Is there a way I can tell
sqlcto addomitemptytag to a particular field? (Description field in this case)Any other way to solve this issue?
Beta Was this translation helpful? Give feedback.
All reactions