How to parse json and display it in CBV.

60 views
Skip to first unread message

tango ward

unread,
Dec 26, 2017, 12:30:45 PM12/26/17
to django...@googlegroups.com

Hi,

I need suggestions on this.

I want to use the OpenDOTA API for my pet project. As a beginner, I don't know where to start working on this. I have found the OpenDOTA API https://api.opendota.com/api/heroes which I am planning to use but I am confuse as to how to parse the json into my CBVs. I read this article https://godjango.com/blog/working-with-json-and-django/ but it doesn't show how to work with json in CBV.

What I would like to achieve is to display all heroes from the OpenDOTA api link into my template.

Can anyone shed some light on where should I start?


Regards,
Jarvis

Andréas Kühne

unread,
Dec 26, 2017, 12:57:22 PM12/26/17
to django...@googlegroups.com
Hi,

If you want to parse json in django - or in python for that matter - all you have to do is to use the json module. 

import json

object = json.loads(insert_string_here)

insert_string_here is the string that you want to parse - and object is the parsed json.


Regards,

Andréas

--
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/CAA6wQL%2BFW8ZGjgCxX2MKNr9e8mmR6xvsmZwSTufTeEFB54tjrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

tango ward

unread,
Dec 26, 2017, 1:08:51 PM12/26/17
to django...@googlegroups.com
Hi Andréas,

Thanks for the help.

Also, I am not sure if the correct term is parse but I also want to display the images of heroes of DOTA 2 from OpenDOTA API. I saw a video on youtube on how to do it however its in Swift. Not sure if I can do it using CBV in django.

Andréas Kühne

unread,
Dec 26, 2017, 1:29:33 PM12/26/17
to django...@googlegroups.com
All you need is to send the image (the url to the image) that should be shown to the template, and then display it on the template. It shouldn't be too hard as long as you understand how to send context data to the template.

See here :

Regards,

Andréas

tango ward

unread,
Dec 26, 2017, 1:43:24 PM12/26/17
to django...@googlegroups.com
Thanks for the idea Andréas. You're always helping me.

tango ward

unread,
Dec 27, 2017, 4:03:21 PM12/27/17
to django...@googlegroups.com
Follow up question. Apologies for the continuation.

I tried integrating the API from OpenDOTA https://api.opendota.com/api/heroes.

my code:

import requests
import json

res = url.json()

when I typed in 'res' in my terminal, it returns a List of dictionaries. Now, is it possible to turn this list to a dictionary? Apologies for the confusion, I am confuse as well.


Regards,
Jarvis



James Schneider

unread,
Dec 27, 2017, 4:45:48 PM12/27/17
to django...@googlegroups.com


On Dec 27, 2017 1:02 PM, "tango ward" <tango...@gmail.com> wrote:
Follow up question. Apologies for the continuation.

I tried integrating the API from OpenDOTA https://api.opendota.com/api/heroes.

my code:

import requests
import json

res = url.json()

when I typed in 'res' in my terminal, it returns a List of dictionaries. Now, is it possible to turn this list to a dictionary? Apologies for the confusion, I am confuse as well.

The terms you are looking for are serialization (converting Python objects to JSON) and deserialization (converting JSON to Python objects). 

If you're using requests, and you can successfully run the .json() method, then you successfully converted the JSON to a native Python structure. 

In this case, you are requesting a list of heroes, so you received a list back. You'll need to find the hero you want in the list using the standard methods of working with Python lists, access that element, and then you'll have access to the dict for that hero.

There is probably a way to get a single dict for a specific hero from the API, but I'm not familiar with their API structure. You'll need to revert to their API documentation to determine the correct URL structure.

-James

tango ward

unread,
Dec 28, 2017, 10:10:20 AM12/28/17
to django...@googlegroups.com
Thanks James.


I heard of serialization in REST but I haven't played around with it. That's why I didn't know the exact terms of what I am doing.

--
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.
Reply all
Reply to author
Forward
0 new messages