2

I have json coming from a url and I want it to be displayed in an html list:

import webapp2
import urllib2
import json
from google.appengine.api import users
from optparse import OptionParser

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCvS6-K6Ydmb4gH-kim3AmjA&maxResults=25&key=AIzaSy...')
   
data = response.read()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(data)

How can I iterate the data variable and show some items in a html list?

1 Answer 1

2

Load the json data into a python dict using json.loads.

import json

...

json_data = response.read()
data = json.loads(json_data)
print('<ul>')
for key in data:
    print('<li> %s = %s </li>', (key, data[key]))
print('</ul>')
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this should fail if the JSON data is a list and not a dictionary, but it could be easily adapted. But this will work for the OPs use case since the YouTube API returns a dictionary (YouTube API docs).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.