Search code examples
google-bigquerynanjson-extract

Does BigQuery support NaN inside Json?


Consider BigQuery Schema "Time=Timestamp, Data=String" and the following two rows

2012-11-01 03:45:23 UTC, { "Sensor1": [45.56], "Sensor2": [45.56], "Sensor3": [34.45]}
2012-11-01 05:45:23 UTC, { "Sensor1": [11.43], "Sensor2": [NaN], "Sensor3": [12.34]}

Then SELECT Time, JSON_EXTRACT(Data, '$.Sensor1') is perfect

2012-11-01 03:45:23 UTC,  [45.56]
2012-11-01 05:45:23 UTC,  [11.43]

but SELECT Time, JSON_EXTRACT(Data, '$.Sensor3') gives

2012-11-01 03:45:23 UTC,  [34.45]
2012-11-01 05:45:23 UTC,  null

Is that expected behaviour? Would I be better off just omitting that NaN from Sensor 2?


Solution

  • This is an expected behavior. As per https://www.json.org/json-en.html, valid JSON values can only be string, number, true or false or null. Hence NaN is interpreted by BigQuery as null since it is considered as an invalid value.

    A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

    If you want retain the value NaN you can enclose it with double quotes ("NaN") but it will be considered as string. Or you can just omit the NaN as you mentioned in your question if you won't be using/needing it.