0

I'm using pandas dataframes to normalize data to post data from a SQL Server database:

res = requests.post(url, json=oportunidade)

And I get the following error code:

Object of type int64 is not JSON serializable

How can I fix that? I've tried to do the following: (aswell as some other code lines but nothing seemed to fix the issue).

df = df.astype('object').dtypes

Code to get data from my SQL Server DB

server_local = 'localhost' 
database_local = 'Local'

conn_local = pyodbc.connect(r'Driver={SQL Server};Server=localhost;Database=Local;Trusted_Connection=yes;')
cursor_local = conn_local.cursor()

query = '''select * from temp'''

df = pd.read_sql(query, conn_local)

My df types look like this:

UA_NUM          int64
UA_EMISSAO      int64
UA_INICIO      object
UA_FIM         object
UA_OPER        object
UA_OPERADO      int64
UA_X_STATU     object
UA_CLIENTE      int64
UA_LOJA        object
UA_TMK         object
UA_X_REVIS     object
UA_X_DIVIS     object
UA_X_REFER     object
UA_DESCNT      object
UA_X_DIMEN     object
UA_XDFOLUP     object
UA_X_FOLUP     object
UA_X_PRVFP      int64
UA_VEND       float64
UA_VEND4       object
A1_COD        float64
A1_NOME        object
A1_EST         object
U7_COD        float64
U7_NOME        object
dtype: object

Here is some sample of the data normalization I used:

for i in range(100):
    for j in range(24):

        if (j == 4):
            if (df.iloc[i][j] == '1'):
                op = 'OP'

And my JSON looks like this (it's too big for me to post the whole thing):

  oportunidade = {
        "oportunidades": [{
            "titulo": "{}".format(df.iloc[i][21]),
            "valor": 0,
            "codigo_vendedor": 1234,
            "codigo_metodologia": 1,
            "codigo_etapa": "{}".format(op),
            "codigo_canal_venda": 1234

If using other methods to extract the data from SQL Server would fix my issue, I'd gladly change the extraction method.

  • Seems like a [duplicate question](https://stackoverflow.com/questions/50916422/python-typeerror-object-of-type-int64-is-not-json-serializable). Did none of the answers provided there work for you? – AlwaysLearning Dec 14 '20 at 13:16
  • I've tried this, and it didn't work, unfortunately, I've looked for other answers aswell and none seemed to do the trick, maybe because I'm using an SQL Query? Thank you for the reply though! – thiagopleasehelp Dec 14 '20 at 13:23
  • I can post my code attempt here, if you think this answer would work then maybe I've applied it wrongly – thiagopleasehelp Dec 14 '20 at 13:31

0 Answers0