Home > Software design >  Object of type function is not JSON serializable in Django
Object of type function is not JSON serializable in Django

Time:09-04

I have been trying to pass this data in json format but i keep getting this error. I am getting all the data and passing it to a payment getaway for processing. But anytime I pass the data i get the error message. Below is the trace message i am getting.

 Traceback (most recent call last):
  File "C:\Users\Robert Yenji\Envs\bob\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Robert Yenji\Envs\bob\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\New folder (2)\project\votedigital\vote\views.py", line 903, in paytes
    data = json.dumps({
  File "c:\python38-32\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "c:\python38-32\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "c:\python38-32\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "c:\python38-32\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type function is not JSON serializable
def paytes(request): 
    amount = 10
    desc = "USSD"
    category = 'Robert'
    phone = "233244491909"
    vote = "10"
    slug = "test"
    award = "test"
    url = 'https://api.pays.co/charge'
    transaction_id = random.randint(100000000000, 999999999999)
    data = json.dumps({
        "reference": transaction_id,
        "amount": amount,
        "phone": phone,
        "email":"[email protected]",
        "mobile_money": {
        "phone" : "0244496709",
        "provider" : "MTN"
         },
        "label" : desc,
        "metadata":{
            "custom_fields": [
                {
                    "value": desc,
                    "nominee_name": slug,
                    "transaction_id": transaction_id,
                    "amount": amount,
                    "award": award,
                    "category": category,
                    "email": email,
                    "phone": phone,
                    "vote": vote,
                }
            ]
        } 
})
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer texk_002929992',
        'Cache-Control': 'no-cache'
    }
    response = requests.post(url, data=data, headers=headers)
    return HttpResponse(response)

CodePudding user response:

Tried dumping your data and it gave me the error "name email is not defined". Changing it to some string solves the issue. So most probably it is an auto import or something else.

CodePudding user response:

Email is not defined

amount = 10
desc = "USSD"
category = 'Robert'
phone = "233244491909"
vote = "10"
slug = "test"
award = "test"
url = 'https://api.pays.co/charge'

"email": email,

  • Related