Kevin D.
Kevin D.

Reputation: 315

Multiple lists in single json output

So I need a certain json format for a chart, I've added the following to the views

views.py

def booking_add_to_timeline(request):
data = Booking.objects.all().values(
    'booking_no',
    'arrival_date',
    'departure_date',
    )

data_list = list(data)

return JsonResponse({"data": data_list})

Which gives me the following output:

JSON

{
  "data": [
    {
      "booking_no": "1",
      "arrival_date": "2018-09-03",
      "departure_date": "2018-09-10",
    },
    {
      "booking_no": "2",
      "arrival_date": "2018-09-12",
      "departure_date": "2018-09-19",
    }
  ]
}

I'm looking to add another list, creating the following:

JSON

{
  "data": [
    {
      "booking_no": "1",
      "arrival_date": "2018-09-03",
      "departure_date": "2018-09-10",
    },
    {
      "booking_no": "2",
      "arrival_date": "2018-09-12",
      "departure_date": "2018-09-19",
    }
  ],
  "room": [
    {
      "nr": "102"
      "persons": "2"
    },
    {
      "nr": "103"
      "persons": "2"
    }
  ]
}

I've tried to add anoter list by combining both list_all = data_list + room_list but that doesn't create a seperate array...

Upvotes: 0

Views: 331

Answers (1)

vishes_shell
vishes_shell

Reputation: 23524

What you should do is combine your values into one dict:

return JsonResponse({"data": data_list, "room": room_list})

This will do what you want.

Upvotes: 1

Related Questions