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 2, 2020 in Python by kartik
• 37,490 points
749 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 2, 2020 by Niroj
• 82,560 points

Related Questions In Python

0 votes
1 answer
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 19, 2019 in Python by Noel Deepak Palle
4,066 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
11,507 views
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,090 points
7,191 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,560 points
583 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,560 points
241 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,560 points
5,341 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,560 points
905 views