For heaven's sake avoid null value in response Json object

Namaskara,

For example in get order details the response is as follows:

{
“status”: “success”,
“data”: {
“exchange”: “NSE”,
“product”: “D”,
“price”: 571.0,
“quantity”: 1,
“status”: “complete”,
“tag”: null,
“instrument_token”: “NSE_EQ|INE062A01020”,
“placed_by”: “******”,
“trading_symbol”: “SBIN-EQ”,
“tradingsymbol”: “SBIN-EQ”,
“order_type”: “LIMIT”,
“validity”: “DAY”,
“trigger_price”: 0.0,
“disclosed_quantity”: 0,
“transaction_type”: “BUY”,
“average_price”: 570.95,
“filled_quantity”: 1,
“pending_quantity”: 0,
“status_message”: null,
“status_message_raw”: null,
“exchange_order_id”: “1300000025660919”,
“parent_order_id”: null,
“order_id”: “231019025562880”,
“variety”: “SIMPLE”,
“order_timestamp”: “2023-10-19 13:25:13”,
“exchange_timestamp”: “2023-10-19 13:25:13”,
“is_amo”: false,
“order_request_id”: “1”,
“order_ref_id”: “GTT-C23191000044253”
}
}

I have higlighted the following Keys:

  1. tag
  2. status_message
  3. status_message_raw
  4. parent_order_id

Now these fields have a possibility of null in value field.

That is a mess to process. If not handled carefully, it leads to exceptions.

Most of your data is either a numeric (int, float) or alpha-numeric (string).

if its a numeric then state a rule that numeric zero (0) means no value. And for string field have “” to indicate that this fields is a blank field (string cannot be null by the way, it can be blank).

0 and “” are very easy to process , most importantly they dont raise an exception.

Please correct this across your api.

null → indicates very poor programming practice.

Regards
Rathnadhar K V

Thanks for this feedback. Generally we try to avoid using 0.0 for “empty” values because we have seen situations where it’s actually useful information.

See this: MCX negative oil futures pricing mess: Jury out on contract law - The Economic Times

In the (very rare) event that certain prices are zero or negative, we didn’t want to publish wrong information.

Of course the fields you have pointed out are not price related but due to this reason, we opted to use null instead of empty string or 0.0 for floating points.

3 Likes

Saying “null is bad programming” is like saying “empty plates are bad cooking.” It’s not bad, it’s just a signal. There’s nothing there. null (or None, nil, etc.) is a standard and valid concept across most programming languages and API designs.

Replacing null with 0 or "" breaks semantic clarity. For example: discount = 0 is different from discount = null (not available).

The possible null values are clearly documented in the API schema. You just have to handle them right to avoid exceptions.