librelist archives

« back to archive

Making JSON Useful

Making JSON Useful

From:
Joshua Finnie
Date:
2011-04-18 @ 02:50
I have been fooling around with the new foursquare venue API and am able to
get nearby venues sent to me for a given coordinate.  In a perfect world, I
would love for the people using my website to be able to then pick which
venue they are at...

This is where I need help...  I cannot for the life of me seem to get the
json data to any useful way to properly interact with Flask.

My code looks like this:

    lat = request.form['lat']
    lon = request.form['lon']
    url = SEARCH_BASE + '?ll=' + lat + ',' + lon + '&client_id=' + CLIENT_ID
+ '&client_secret=' + CLIENT_SECRET
    req = urllib2.Request(url)
    opener = urllib2.build_opener()
    results = opener.open(req)
    return results.read()

which just barfs out the json code.  How do I get the json to properly
interact with a template so that I can create a list of each venue so that
people can pick from it?

I put the json here (http://paste.pocoo.org/show/373671/) so you know what I
am dealing with...

Hope this question is coherent.

**************************************************************
Joshua Finnie
Central Connecticut State University
Masters of Science in Geography
24 Park Pl Apt B8H
Hartford, CT 06106
(860) 716-5996
**************************************************************

Re: [flask] Making JSON Useful

From:
Rob Mela
Date:
2011-04-18 @ 14:17
Load the json into a dictionary and pass the dictionary to a Jinja template.

The data your loading has a pitfall of having a dictionary element called 'items'.


Here's an all-in-one example here to be run from command line.  In real 
use you would have a template file on disc -- 'foo.html' and pass the 
template file name to render_template ( see Flask docs )



import urllib
import json
import jinja2

t=jinja2.Template("""

{% for group in response.groups %}
        Group {{group.name}}
        {% for item in group['items'] %}
                {{item.name}} {{ item.location.city }}
        {% endfor %}
{% endfor %}

""")

d=json.loads(urllib.urlopen('http://paste.pocoo.org/raw/373671/').read())
print t.render(response=d.get('response'))


Den Apr 17, 2011 kl. 10:50 PM skrev Joshua Finnie:

I have been fooling around with the new foursquare venue API and am able 
to get nearby venues sent to me for a given coordinate.  In a perfect 
world, I would love for the people using my website to be able to then 
pick which venue they are at...

This is where I need help...  I cannot for the life of me seem to get the 
json data to any useful way to properly interact with Flask.

My code looks like this:

    lat = request.form['lat']
    lon = request.form['lon']
    url = SEARCH_BASE + '?ll=' + lat + ',' + lon + '&client_id=' + 
CLIENT_ID + '&client_secret=' + CLIENT_SECRET
    req = urllib2.Request(url)
    opener = urllib2.build_opener()
    results = opener.open(req)
    return results.read()

which just barfs out the json code.  How do I get the json to properly 
interact with a template so that I can create a list of each venue so that
people can pick from it?

I put the json here (http://paste.pocoo.org/show/373671/) so you know what
I am dealing with...

Hope this question is coherent.

**************************************************************
Joshua Finnie
Central Connecticut State University
Masters of Science in Geography
24 Park Pl Apt B8H
Hartford, CT 06106
(860) 716-5996
**************************************************************

Re: [flask] Making JSON Useful

From:
Andy Wilson
Date:
2011-04-18 @ 03:12
On Sun, Apr 17, 2011 at 9:50 PM, Joshua Finnie <joshua.finnie@gmail.com>wrote:

> I have been fooling around with the new foursquare venue API and am able to
> get nearby venues sent to me for a given coordinate.  In a perfect world, I
> would love for the people using my website to be able to then pick which
> venue they are at...
>
> This is where I need help...  I cannot for the life of me seem to get the
> json data to any useful way to properly interact with Flask.
>
> My code looks like this:
>
>      lat = request.form['lat']
>     lon = request.form['lon']
>     url = SEARCH_BASE + '?ll=' + lat + ',' + lon + '&client_id=' +
> CLIENT_ID + '&client_secret=' + CLIENT_SECRET
>     req = urllib2.Request(url)
>     opener = urllib2.build_opener()
>     results = opener.open(req)
>     return results.read()
>
> which just barfs out the json code.  How do I get the json to properly
> interact with a template so that I can create a list of each venue so that
> people can pick from it?
>
> I put the json here (http://paste.pocoo.org/show/373671/) so you know what
> I am dealing with...
>
> Hope this question is coherent.
>
> **************************************************************
> Joshua Finnie
> Central Connecticut State University
> Masters of Science in Geography
> 24 Park Pl Apt B8H
> Hartford, CT 06106
> (860) 716-5996
> **************************************************************
>



Try using the json or simplejson module to parse the json response into a
python object and pass that to your template(s):
http://docs.python.org/library/json.html#json.load

simplejson is the same api, but works for python versions < 2.6 and is more
frequently updated: http://simplejson.github.com/simplejson/

Re: [flask] Making JSON Useful

From:
Kenneth Reitz
Date:
2011-04-18 @ 03:16
Also, i wrote a cool module for making http requests a lot easier called 
'requests' that you might find useful :)

http://kennethreitz.com/blog/introducing-requests/




-- 
Kenneth Reitz
http://kennethreitz.com/contact-me
On Sunday, April 17, 2011 at 11:12 PM, Andy Wilson wrote: 
> 
> On Sun, Apr 17, 2011 at 9:50 PM, Joshua Finnie <joshua.finnie@gmail.com> wrote:
> >  I have been fooling around with the new foursquare venue API and am 
able to get nearby venues sent to me for a given coordinate. In a perfect 
world, I would love for the people using my website to be able to then 
pick which venue they are at...
> > 
> > This is where I need help... I cannot for the life of me seem to get 
the json data to any useful way to properly interact with Flask.
> > 
> > My code looks like this:
> > 
> > lat = request.form['lat']
> > lon = request.form['lon']
> > url = SEARCH_BASE + '?ll=' + lat + ',' + lon + '&client_id=' + 
CLIENT_ID + '&client_secret=' + CLIENT_SECRET
> > req = urllib2.Request(url)
> > opener = urllib2.build_opener()
> > results = opener.open(req)
> > return results.read()
> > 
> > which just barfs out the json code. How do I get the json to properly 
interact with a template so that I can create a list of each venue so that
people can pick from it? 
> > 
> > I put the json here (http://paste.pocoo.org/show/373671/) so you know 
what I am dealing with...
> > 
> > Hope this question is coherent. 
> > **************************************************************
> > Joshua Finnie
> > Central Connecticut State University
> > Masters of Science in Geography
> >  24 Park Pl Apt B8H
> > Hartford, CT 06106
> > (860) 716-5996
> > **************************************************************
> 
> 
> 
> Try using the json or simplejson module to parse the json response into 
a python object and pass that to your template(s): 
http://docs.python.org/library/json.html#json.load 
> 
> simplejson is the same api, but works for python versions < 2.6 and is 
more frequently updated: http://simplejson.github.com/simplejson/ 
> 
>