Skip to content
Advertisement

How to cast from double to int in from_json Spark SQL (NULL output)

I have a table with a JSON string

When running this Spark SQL query:

I get a NULL, since the data types for some_number are not matching (int vs double)…

If I run this it works:

Is there a way to CAST this on-the-fly?

Advertisement

Answer

You can do from_json first using array<struct<column_1:string,some_number:double>> then cast as array<struct<column_1:string,some_number:int>>

Example:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement