Python Python - Endpoint : Can the function not return an array of dictionaries ?

Click For Summary
The discussion revolves around a Python server code that processes a CSV file using pandas to retrieve information about islands belonging to a specific municipality. The user encounters a "TypeError: Object of type int64 is not JSON serializable" when trying to return data as JSON. This error is attributed to the int64 data type used by pandas, which is not compatible with JSON serialization. A suggested solution involves converting int64 values to standard Python integers before returning them in the JSON response. The conversation highlights the need for careful handling of data types when working with CSV files and JSON in Python, noting that pandas defaults to using int64 and float64 types, which may require conversion for proper serialization.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

I am trying to write a code for a server in Python and I got stuck.

I gave as an input a csv file and using pandas we get a dictionary where the titles are the keys and the inputs are the values.

From that we get the below :

1653924226377.png


I have written the below endpoint to get all the islands that belog to the specific municipality :

Code:
@app.route("/municipality/<municipality>",methods=["GET"])
def get_island_municipality(municipality): 
    dict_to_send = {} 
    dict_to_send["data"] = get_islands_municipality(municipality)
    dict_to_send["message"]=f'These are the islands that belong to {municipality}'
    return jsonify(dict_to_send)
and the function I have used is the below one :

Code:
def get_islands_municipality(municipality): 
    islands = []  
    for i in range(len(dataframe["Municipality"])) : 
        islands_dict = {} 
        if dataframe["Municipality"][i] == municipality :  
            islands_dict["Island's Name"] = dataframe["Island's Name"][i] 
            islands_dict["Municipality"] = dataframe["Municipality"][i] 
            islands_dict["Amount of Hotels"] = dataframe["Amount of Hotels"][i]  
            islands_dict["Amount of Beaches"] = dataframe["Amount of Beaches"][i]  
            islands.append(islands_dict)
    return islands

but I get an error, I suppose that the error relates to the type of my return. I return an array of dictionaries, right? Is that not allowed? The error that I get is "TypeError: Object of type int64 is not JSON serializable".

:unsure:
 
Technology news on Phys.org
I had to look this up but I found a couple threads on StackExhange which reference this error and they both say that this is due to the int64 type. That is a numpy type, so the fix seems to be converting it to a Python int before dumping to JSON. I would try modifying your function to something like this maybe.

Code:
def get_islands_municipality(municipality):
    islands = [] 
    for i in range(len(dataframe["Municipality"])) :
        islands_dict = {}
        if dataframe["Municipality"][i] == municipality : 
            islands_dict["Island's Name"] = dataframe["Island's Name"][i]
            islands_dict["Municipality"] = dataframe["Municipality"][i]
            islands_dict["Amount of Hotels"] = int(dataframe["Amount of Hotels"][i])
            islands_dict["Amount of Beaches"] = int(dataframe["Amount of Beaches"][i])
            islands.append(islands_dict)
    return islands
 
Jameson said:
I had to look this up but I found a couple threads on StackExhange which reference this error and they both say that this is due to the int64 type. That is a numpy type, so the fix seems to be converting it to a Python int before dumping to JSON. I would try modifying your function to something like this maybe.

Code:
def get_islands_municipality(municipality):
    islands = []
    for i in range(len(dataframe["Municipality"])) :
        islands_dict = {}
        if dataframe["Municipality"][i] == municipality :
            islands_dict["Island's Name"] = dataframe["Island's Name"][i]
            islands_dict["Municipality"] = dataframe["Municipality"][i]
            islands_dict["Amount of Hotels"] = int(dataframe["Amount of Hotels"][i])
            islands_dict["Amount of Beaches"] = int(dataframe["Amount of Beaches"][i])
            islands.append(islands_dict)
    return islands
Now it works properly! :geek: Does this happen because in the cvs filrthe numbers are not defined to beof type int? Or why does this happen?
So every time we have numbers in a csv file and we want to return these values we have to make them int first? :unsure:
 
I think so unfortunately. Here are the types that the JSON format can work with and it looks like pandas has default behavior to use int64 and float64. You could write a function to loop over your columns and column types and convert any int64 dtypes to int as well as float64 to float.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 55 ·
2
Replies
55
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K