Shawn Shawn - 1 year ago 84
JSON Question

JSON: handle null value from MySQL export

I export data from MySQL into a JSON file, but there is an error due to null value. How do I handle this?

SQL query:

select industry, ... from table1;

where some of the industry gives null value:

enter image description here

JSON format

[{"industry":"entertainment", ...}, {"industry":"", ...}, {"industry":NULL, ...}]

The error is due to the 3rd object value in the array (i.e. NULL)
enter image description here

I tried:

select cast(industry as char), ... from table1;

Unfortunately, it still gives the same error. Because in the MySQL cell, it shows NULL value.



As there will be other variables in each object, I would like to change the NULL value in SQL either to "" or "NULL".

I export the data from MySQL using the highlighted button:

enter image description here


You can use ifnull to cast your NULL values

select ifnull(industry, '') from table1;