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

AI Thread 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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
4
Views
2K
Replies
2
Views
1K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
2
Views
22K
Replies
7
Views
4K
Back
Top