How to return multiple dataframes as JsonResponse in Django?

750 Views Asked by At

I have 2 dataframes. I have to send these two dataframes in json format whenever the django API is called.

c = a.to_json(orient='records')
d = b.to_json(orient='records')
return JsonResponse(json.loads(c,d),safe = False)

a and b are dataframes

I'm getting error when I'm executing the above code. How to send the 2 separate dataframes in json format.

1

There are 1 best solutions below

3
On

You need to put the dataframes into some sort of structure that can then be serialized back to json. One way would be:

c = json.loads(a.to_json(orient='records'))
d = json.loads(b.to_json(orient='records'))
return JsonResponse([c, d], safe = False)

This will avoid having a double-serialized string.