0

I am trying to load a python data framework to Watson Function Action. I got the following error.

{ "error": "Internal error. Object of type module is not JSON serializable" }

I am loading a Watson Notebook Dataframe in Watson Function Action creating a function that returns a Json, from the notebook I test it and it works if the Json returns but I do not know if I need something in the function

Dataframe code

dfData = dfData[['SOURCE','NUMBER_ID','DATE_OPEN','DATE_CLOSE','COMPANY','CATEGORY','SUBCATEGORY','LOGICAL_NAME','CLR_TXT_SERVICIO','STATUS',]]

jsondf = dfData.to_dict(orient='record')
print(jsondf)

I have tried with:

#jsondf = dfData.to_json( orient='records')
print(jsondf)

#jsondf = jsondf.dumps(dfData, cls=JSONEncoder)
print(jsondf)

But none works

import sys
import types
import pandas as pd
import numpy as np
import ibm_boto3
import json as jsondf
import dateutil.relativedelta
from pandas import DataFrame
from botocore.client import Config
from datetime import datetime, date, time, timedelta


def main(dict):
    def __iter__(self): 

        client_22e74e133ed74557a9183bca634893be = ibm_boto3.client(service_name='s3',
        ibm_api_key_id='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        ibm_auth_endpoint="https://iam.bluemix.net/oidc/token",
        config=Config(signature_version='oauth'),
        endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')


        streaming_body_14 = client_22e74e133ed74557a9183bca634893be.get_object(Bucket='clarotp-donotdelete-pr-jfrmyss7bazrmn', Key='Vista_Mayo.xlsx')['Body']
        # add missing __iter__ method so pandas accepts body as file-like object
        if not hasattr(streaming_body_14, "__iter__"): streaming_body_14.__iter__ = types.MethodType( __iter__, streaming_body_14 ) 


        #convertir el streaming body del archivo Excel a dataframe
        def load_df(fileobject,sheetname):
            excelFile = pd.ExcelFile(fileobject)
            dfData = excelFile.parse(sheetname)
            return dfData

        #se crea el dateframe (dfData)
        dfData = load_df(streaming_body_14, 'Exportar Hoja de Trabajo')

        dfData = dfData[dfData['NUMBER_ID'].str.contains('IM1010935', case=False)]

        jsondf = dfData.to_dict(orient='record')


    return  {'message': [jsondf] } 

This is the function that I created but it does not work

Results:

{ "error": "Internal error. Object of type module is not JSON serializable" } Logs: []

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

1 Answers1

0

Without a complete traceback it's difficult to know which line of code is causing the problem. However, from this error message:

Object of type module is not JSON serializable

we know that somewhere the code is trying to convert a module object to json.

import json as jsondf

this line

return  {'message': [jsondf] }

Given your current code, this line is equivalent to

>>> m = {'message': [jsondf]}
>>> m
{'message': [<module 'json' from '/usr/local/lib/python3.7/json/__init__.py'>]}

Note that the value contains a module object, which cannot be serialised to json.

>>> jsondf.dumps(m)
Traceback (most recent call last):
  ...
TypeError: Object of type module is not JSON serializable

Perhaps the return statement should be indented, so that jsondf is the value assigned in this line?

jsondf = dfData.to_dict(orient='record')

So you have

    jsondf = dfData.to_dict(orient='record')
    return  {'message': [jsondf] }

not

    jsondf = dfData.to_dict(orient='record')
return  {'message': [jsondf] }

In any case, I recommend that you don't use the names of modules that you have imported as variables in your code.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153