URL JSON Response using Django Rest Framework

I have an URL let’s say (http://json-data.com) that returns a JSON response. I have to fetch this JSON data from this URL and save this data into an RDBMS.

So is there any method or class available from Django or REST Framework I can use to get the JSON data from an online URL in a variable so that I can then save it to RDBMS?

I am new to python and Djanog so maybe this question is naive. I would be grateful if you can assist me in any way.

Thanks in advance

Answer

You could use a JSONField on your model if you just want to store raw JSON in your database. Have a look at the docs here

If you want to store the returned values on a instance of your model, you could do something like this:

data = requests.get(url)
json_data = data.json()

# isolate the data key from the HTTP response object
item_list = json_data.get('data')

for item in item_list:
  name = item['name']
  age = item['age']

  # This will create a new instance for every object in the array from JSON response
  YourModel.objects.create(name=name, age=age)