Map object is not JSON serializable Map object is not JSON serializable json json

Map object is not JSON serializable


map() in Python 3 is a generator function, which is not serializeable in JSON. You can make it serializeable by converting it to a list:

from django.http import JsonResponsefrom collections import OrderedDictdef order(request):        bunch = OrderSerializer(Order.objects.all(), many=True)    headers = bunch.data[0].keys()    # consume the generator and convert it to a list here    headers_prepared = list(map(lambda x: {'data': x} , headers))    ordered_all = (('columns', headers_prepared), ('lines', bunch.data))    data = OrderedDict(ordered_all)    return JsonResponse(data)


if someone come across this problme when using map(),you can try using list(map()) to solve this problem.