Error map object at 0x7fa3435f3048 is not JSON serializable

+1 vote

 when returning a JSONResponse, which was added in Django 1.7. and is a wrapper around json.dumps. However, in this case it results in an error. I'm sure the data is correct and can be serialized to JSON through Python shell.

What is the right way to serialize the data to JSON?

from django.http import JsonResponse
from collections import OrderedDict

data = OrderedDict([('doc', '546546545'), ('order', '98745'), ('nothing', '0.0')])

return JsonResponse(data) # doesn't work, JSONRenderer().render(data) works

Results in this error:

<map object at 0x7fa3435f3048> is not JSON serializable

print(data) gives:

OrderedDict([('doc', '546546545'), ('order', '98745'), ('nothing', '0.0')])

Jul 3, 2020 in Python by kartik
• 37,520 points
2,500 views

1 answer to this question.

0 votes

Hello @kartik,

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 JsonResponse
from collections import OrderedDict

def 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)
answered Jul 3, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer
0 votes
0 answers

Python - TypeError: Object of type 'int64' is not JSON serializable

My data frame has store names and ...READ MORE

May 26, 2022 in Python by Kichu
• 19,040 points
2,533 views
0 votes
1 answer

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 20, 2019 in Python by Noel Deepak Palle
5,891 views
0 votes
1 answer

Python error "'Series' object is not callable "

Try this: df['ln_returns'] = np.log(df['Close_mid']/df['Close_mid']) df['Close_mid'](1)) doesn't seem to ...READ MORE

answered Jul 22, 2019 in Python by Greg
17,466 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,437 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,555 views
0 votes
1 answer

Django object is not JSON serializable

Hello @kartik, simplejson and json don't work with django objects well. Django's ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,840 points
27,604 views
0 votes
1 answer

How to convert a DictProxy object into JSON serializable dict?

Hello, Rather than using a private DictProxy method like _getvalue(), I'd ...READ MORE

answered Apr 15, 2020 in Python by Niroj
• 82,840 points
5,138 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP