Filter Json boolean data

43 views
Skip to first unread message

tango ward

unread,
Jan 19, 2018, 5:51:42 AM1/19/18
to django...@googlegroups.com
Hi guys,


I want to get the correct data if if key == false from a json data. My if else condition seems to be working however the data is inconsistent. Here's my code:


class DotaMatches(TemplateView):
    template_name = 'matches/dota_matches.html'

    def get(self, request, *args, **kwargs):
        # Get the URL of the API using requests module
        url = requests.get('https://api.opendota.com/api/proMatches')
        # Decode the data
        json_data = url.json()

        # List that will contain dictionaries
        game_data = []

        for data in json_data:
            # Will contain the values from the dictionary data of json_data
            j_data = {}
            j_data['League'] = data['league_name']
            j_data['Radiant'] = data['radiant_name']
            j_data['Dire'] = data['dire_name']
            j_data['Duration'] = str(timedelta(seconds=data['duration']))
            j_data['Finished'] = timeago.format(datetime.now() - datetime.fromtimestamp(data['start_time']))

            # Check who the winner of the match
            if data['radiant_win'] == 'False':
                j_data['Winner'] = data['dire_name']
            else:
                j_data['Winner'] = data['radiant_name']

            # Put the data in our game_data list
            game_data.append(j_data)

        return render(request, self.template_name, {'game_data': game_data})


Not sure if there's something that I need to do first before running the if - else condition.



Regards,
Jarvis

Florian Schweikert

unread,
Jan 19, 2018, 6:33:43 AM1/19/18
to django...@googlegroups.com
Hi,

On 19/01/18 11:50, tango ward wrote:
>             # Check who the winner of the match
>             if data['radiant_win'] == 'False':

You are comparing a boolean with the *string* 'False', remove the ' and
try again.

tango ward

unread,
Jan 19, 2018, 1:30:50 PM1/19/18
to django...@googlegroups.com
That works. Silly me.

Thank you Florian.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/28f8e57a-2857-b5dc-c987-d4f378bb3247%40ist-total.org.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages