Nilay Singh
Nilay Singh

Reputation: 2341

Django display list data in json

I am using django to display pandas data frame into json format I am suing django panda I can convert my model into data frame but I don't know how to display whole frame in json format. I am using this code:

def index(request):
qs = DebtManagement1S.objects.all()
df = qs.to_dataframe()
resp_data = {
    'x': list(df['sector']),
    'z': df.to_json()
    }
return JsonResponse(resp_data)

And it is returning Json response In my x value I can get a result in list but when I am doing df.to_json() it is returning value in string and I can't use it as a reponse. I want to return a proper json response where I can access it. Is there any way to convert it into json format and return values like this:

 {
   sector: {
            "Internal Debt",
             "Central Loans"
         ...
           }
  }

Upvotes: 0

Views: 230

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600041

Your resp_data dict needs to consist of Python data structures. So you should call df.to_dict().

Upvotes: 1

Related Questions