Convert MySQL decimal to an acceptable format so that you can serialize it

Possible duplicate:
Python JSON serialize decimal object

I have the following SQL:

SELECT concat(UNIX_TIMESTAMP(date), '000') as datetime, SUM(royalty_price) as sales FROM sales_raw GROUP BY datetime 

The results look something like this:

 datetime sales 1337151600000 1045.71 (decimal) 1337238000000 478.04 1337324400000 300.96 1337410800000 289.02 

From this I get the following error:

 Exception Type: TypeError at /ajax/graph/ Exception Value: Decimal('1045.71') is not JSON serializable 

How to convert decimal to acceptable format in mysql to be able to serialize this data?

+4
source share
1 answer

If you must do this in MySQL, just CAST to the line:

 SELECT CONCAT(UNIX_TIMESTAMP(date), '000') AS datetime, CAST(SUM(royalty_price) AS CHAR) AS sales FROM sales_raw GROUP BY datetime 
+4
source

Source: https://habr.com/ru/post/1415441/


All Articles