Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Api design: Which is better: returning null for empty values or nothing

I'm designing my first API for a web app based on a MongoDB datastore. I return json responses and I don't know how to decide, which is better:

Keeping a consistent response scheme with empty fields or null. Or returning only non-empty values.

That imply for examples, when removing values from an array on my Mongo, I need to check if the array is empty, and if so, $unset it.

like image 836
Jason Lince Avatar asked Dec 22 '22 05:12

Jason Lince


1 Answers

I prefer to avoid nulls for relational schemas and application code; I'd recommend the same for NoSQL.

"null" means "I don't know" to me.

An empty object says "You asked for this, but nothing was there." I think it's more explicit and correct.

like image 200
duffymo Avatar answered Apr 28 '23 19:04

duffymo