I’m using the following SQL query to get data for every month in a given year:
JavaScript
x
5
1
SELECT DATE_FORMAT(tour_date , '%M'), COUNT(*)
2
FROM _673338137185
3
WHERE tour_date LIKE '{tour_year}%'
4
GROUP BY DATE_FORMAT(tour_date , '%M')
5
When I’m returning this via Python, I’m getting the following result:
JavaScript
1
45
45
1
[
2
[
3
[
4
"April",
5
9
6
],
7
[
8
"August",
9
5
10
],
11
[
12
"February",
13
3
14
],
15
[
16
"July",
17
6
18
],
19
[
20
"June",
21
3
22
],
23
[
24
"March",
25
1
26
],
27
[
28
"May",
29
8
30
],
31
[
32
"November",
33
1
34
],
35
[
36
"October",
37
2
38
],
39
[
40
"September",
41
4
42
]
43
]
44
]
45
Also, there are n everywhere in the result. I need the result in JSON format, but I can’t get it right. How can I do it?
Advertisement
Answer
If l
is the list you display, simple use json.dumps
:
JavaScript
1
7
1
import json
2
3
print(json.dumps(l))
4
5
# Output
6
[[["April", 9], ["August", 5], ["February", 3], ["July", 6], ["June", 3], ["March", 1], ["May", 8], ["November", 1], ["October", 2], ["September", 4]]]
7