turning nested json to django model

hi, i wrote a webhook receiver that receives response from an a API. naturally, the response is in the form of a nested json data.

“event”: “subscription.create”,
“data”: {
“domain”: “test”,
“status”: “active”,
“subscription_code”: “Sblabla”,
“amount”: 50000,
“cron_expression”: “0 0 28 * *”,
“next_payment_date”: “2016-05-19T07:00:00.000Z”,
“open_invoice”: “none”,
“createdAt”: “2016-03-20T00:23:24.000Z”,
“plan”: {
“name”: “Monthly retainer”,
“plan_code”: “blabla”,
“description”: “subscription”,
“amount”: 50000,
“interval”: “monthly”,
“send_invoices”: ‘’,
“send_sms”: ‘’,
“currency”: “NGN”
“customer”: {
“first_name”: “BoJack”,
“last_name”: “Horseman”,
“email”: “bojack@horsinaround.com”,
“customer_code”: “CUSblbla”,
“phone”: “”,
“metadata”: {},
“risk_action”: “default”
“created_at”: “2016-10-01T10:59:59.000Z”

how do i extract some data and save them into a django model

You’ve got at least two options:

  1. Use the Python json.loads method to convert the data received to a Python dict. From there you can copy the different methods to the fields you need.
  2. You can use the serialization methods in the Django Rest Framework to do the heavy lifting for you. It might take some more work to get it set up, but might be more reliable overall.


thanks, ken. can you point me to an article or video?

The Django Rest Framework docs are a good place to start. There are examples showing how to use it.

Beyond that, I don’t have any specific recommendations. You might want to take a look at Will Vincent’s Awesome Django page to help find some resources. I also found an Awesome Django Rest Framework page that has some more DRF-specific resources identified.