IPO
IPO

Reputation: 835

Saving a JSON list - Django

I am working with a JSON request.get that returns a list. I will like to save each individual object in the response to my models so I did this:

in views.py:

def save_ram_api(request):
r = requests.get('https://ramb.com/ciss-api/v1/')

# data = json.loads(r)

data = r.json()

for x in data:

    title = x["title"]
    ramyo_donotuse = x["ramyo"]
    date = x["date"]
    thumbnail = x["thumbnail"]
    home_team_name = x["side1"]["name"]
    away_team_name = x["side2"]["name"]
    competition_name = x["tournament"]["name"]

ramAdd = ramSample.objects.create(title=title, ramyo_donotuse=ramyo_donotuse, date=date, thumbnail=thumbnail, home_team_name=home_team_name, away_team_name=away_team_name, competition_name=competition_name)

ramAdd.save()

return HttpResponse("Successfully submitted!") 

This works fine except that it would only save the last objects on the list.

the JSON response list (as a random 60 objects at any time) would look something like:

[
    {
        "title": "AY - BasketMouth",
        "ramyo": "AY de comedian"
        "side1": {
            "name": "Comedy Central",
            "url": "https:\/\/www.rabithole.com\/laugh\/dave-chappel\/"
        },
        "side2": {
            "name": "Basket Mouth",
            "url": "https:\/\/www.rabithole.com\/laugh\/chris-rockie\/"
        },
        "tournament": {
            "name": "Night of a thousand laugh",
            "id": 15,
            "url": "https:\/\/www.rabithole.com\/laugh\/chris-rockie\/"
        },
        "points": [
            {
                "nature": "Gentle",
                "phrase": "Just stay"
            },
            {
                "nature": "Sarcastic",
                "phrase": "Help me"
            }
        ]
    },
   {
        "title": "Dave - Chris",
        "ramyo": "Dave Chapelle"
        "side1": {
            "name": "Comedy Central",
            "url": "https:\/\/www.rabithole.com\/laugh\/dave-chappel\/"
        },
        "side2": {
            "name": "Chris Rockie",
            "url": "https:\/\/www.rabithole.com\/laugh\/chris-rockie\/"
        },
        "tournament": {
            "name": "Tickle me",
            "id": 15,
            "url": "https:\/\/www.rabithole.com\/laugh\/chris-rockie\/"
        },
        "points": [
            {
                "nature": "Rogue",
                "phrase": "Just stay"
            }
        ]
    }
]

In this case my views.py will only save the last dictionary on the list, ignoring the other 59.

My question would be:

  1. How do I get the views.py to save the entire objects on the list?

  2. Notice that the "points" is also a list that contains one or more dictionaries, any help how to save this as well?

Upvotes: 1

Views: 1742

Answers (1)

Nahidur Rahman
Nahidur Rahman

Reputation: 815

Your code is saving only the last object in the list because you are creating and saving the object outside of the loop. Try this,

def save_ram_api(request):
    r = requests.get('https://ramb.com/ciss-api/v1/')

    # data = json.loads(r)

    data = r.json()

    for x in data:

        title = x["title"]
        ramyo_donotuse = x["ramyo"]
        date = x["date"]
        thumbnail = x["thumbnail"]
        home_team_name = x["side1"]["name"]
        away_team_name = x["side2"]["name"]
        competition_name = x["tournament"]["name"]

        ramAdd = ramSample.objects.create(title=title, ramyo_donotuse=ramyo_donotuse, date=date, thumbnail=thumbnail, home_team_name=home_team_name, away_team_name=away_team_name, competition_name=competition_name)

        ramAdd.save()

    return HttpResponse("Successfully submitted!")
  1. How do I get the views.py to save the entire objects on the list?

  2. Notice that the "points" is also a list that contains one or more dictionaries, any help how to save this as well?

Regarding your those questions

If you are using PostgreSQL as a database then you can use Django's built is JSONField and ArrayField for PostgreSQL database.

And if your database is not PostgreSQL you can use jsonfield library.

Upvotes: 1

Related Questions