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

In summary: This would make your code a lot easier and ensure that everything works as it should.In summary, the code is not working because the type of the return is not valid.
  • #1
mathmari
Gold Member
MHB
5,049
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
  • #2
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
 
  • #3
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:
 
  • #4
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.
 

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

Can I use Python to create web endpoints?

Yes, Python is a versatile programming language that can be used to create web endpoints. It has many libraries and frameworks, such as Flask and Django, that make it easy to build web applications.

What is an endpoint in Python?

In Python, an endpoint is a function or method that receives a request from a client and returns a response. It is typically used in web development to handle requests from users and provide data or perform actions.

Can a function in Python return an array of dictionaries?

Yes, a function in Python can return an array of dictionaries. This is a common way to return data from an endpoint, as it allows for a structured and organized response.

How do you create an endpoint in Python?

To create an endpoint in Python, you can use a web framework such as Flask or Django. These frameworks provide built-in functions and decorators that allow you to define endpoints and handle requests from clients.

Are there any limitations to what an endpoint can return in Python?

The limitations of what an endpoint can return in Python will depend on the specific framework or library being used. However, in general, an endpoint can return a variety of data types, including strings, numbers, lists, dictionaries, and custom objects.

Similar threads

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