3
def addData():
    res = []
    class InfoData:
        def __init__(x, ID, number):
            x.ID = ID
            x.number = number

    for i in range(0, 10):
        res.append(InfoData("A",i))
        
    return json.dumps(res)
    #I got "Object of type InfoData is not JSON serializable"

I make an API using Flask Python and having a basic function above.

How can I return JSON response from this function ?

2 Answers 2

3

Do it this way.

def addData():
    res = []
    class InfoData:
        def __init__(x, ID, number):
            x.ID = ID
            x.number = number

    for i in range(0, 10):
        res.append(InfoData("A",i).__dict__)
        
    return json.dumps(res)

Sign up to request clarification or add additional context in comments.

Comments

2

Pass json-representation of InfoData class instances. F.e. {'id': ..., 'number': ...}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.