Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object of type function is not JSON serializable

Tags:

python

I currently struggle to understand why the following code is not working:

import json
import random

def _time_series_prc(start_date, count, periodicity, is_history=True):
    values = []

    print(type(start_date))
    print(type(periodicity))

    for i in range(count - 1):
        value = random.uniform(0, 1)
        values.append(value)

    return _build_series(values, start_date, periodicity, is_history)


def _build_series(values, start_date, periodicity, is_history):
    if is_history:
        values.reverse()

    return {
        'periodicity': periodicity,
        'startDate': start_date,
        'values': values,
    }

result = _time_series_prc('2019-07-17', 52, 'WEEKLY')

print(json.dumps(result, indent=4));

output:

<class 'str'>
<class 'str'>
TypeError: Object of type function is not JSON serializable

At line 7, the json.dumps I get the error: TypeError: Object of type function is not JSON serializable`. Im not using ptyhon very long But I cant understand how this can be a function pointer instead of the returned value.

like image 651
Mulgard Avatar asked Jul 31 '26 23:07

Mulgard


1 Answers

Your code works for me on Python 3.7.3.

Also note that for i in range(count - 1): is going to give you 51 results instead of 52 in this case (print(len(result['values']))). Also try removing the ; in your last line of code just in case :)

like image 143
mtj92 Avatar answered Aug 03 '26 13:08

mtj92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!