1

Situation: I am building a Web API that queries a database and then returns the results via JSON.

There are a couple of columns that are being introduced to the table, but do not have data populated in them yet. So their value is "NULL" for the time being. It wont always be this way, and some "legacy" data my contain the "NULL" value going forward.

When I get the JSON response that contains the "NULL" value it is empty.

({foo: , foo1: bar1})

So my question is what is the correct way to pass a "NULL" value back to the original caller? I can all ways check for "NULL" and then return an empty string or "-1" for integers, but I was wondering what the correct method is outside of not having a "NULL" in the first place.

flyingweseal
  • 51
  • 2
  • 8
  • It'd probably be helpful if you included some code... – dmeglio Dec 18 '15 at 22:05
  • 1
    You can ignore null values using Json.Net decorator: [JsonProperty( NullValueHandling = NullValueHandling.Ignore )]. – jpgrassi Dec 18 '15 at 22:08
  • @dman2306 How would adding code help? This a question about programming practice. The code works fine. I'm wondering if there is a better way to handle "null" values within JSON other then passing back an empty string. Is there a standard? – flyingweseal Dec 18 '15 at 22:09
  • @jpgrassi I don't necessarily want to ignore it. I want to pass the key back whether there is a null or not, but I want to be able to inform the user that there is a "null"/empty value for the column. Like I said I can do this with an empty string or a -1, but is that the standard for passing empty values back to the user? – flyingweseal Dec 18 '15 at 22:12
  • 1
    This could help you: http://stackoverflow.com/questions/21120999/representing-null-in-json – jpgrassi Dec 18 '15 at 22:14
  • @jpgrassi will you post that link as an answer so I can mark it and give you credit. – flyingweseal Dec 18 '15 at 22:20

1 Answers1

1

There's a discussion about how to handle null values in JSON here:

Representing null in JSON

I also advise you to be aware of extra fields that you know will be always null or that you will not use (as you said "legacy" data). They are going to count on your JSON's final size so if you are sure they are not going to be used, just remove them.

Community
  • 1
  • 1
jpgrassi
  • 5,482
  • 2
  • 36
  • 55