0

I'm having some trouble in understanding how jsonify works even though I went through the documentation. As you can see below, I'm calling the lookup() function which returns a dictionary, then I'm trying to jsonify it.

@app.route("/articles")
def articles():

    a = lookup(33496)
    return jsonify([link=a["link"], title = a["title"]])       #invalid syntax error

my helpers.py:

import feedparser
import urllib.parse

def lookup(geo):
    """Looks up articles for geo."""       #this function already parses the 'link' and 'title' form rss feed

    # check cache for geo
    if geo in lookup.cache:
        return lookup.cache[geo]

    # get feed from Google
    feed = feedparser.parse("http://news.google.com/news?geo={}&output=rss".format(urllib.parse.quote(geo, safe="")))

    # if no items in feed, get feed from Onion
    if not feed["items"]:
        feed = feedparser.parse("http://www.theonion.com/feeds/rss")

    # cache results
    lookup.cache[geo] = [{"link": item["link"], "title": item["title"]} for item in feed["items"]]

    # return results
    return lookup.cache[geo]

# initialize cache
lookup.cache = {}

The error that I'm getting is of invalid syntax. Any idea into what I'm doing wrong? Thanks

tadm123
  • 8,294
  • 7
  • 28
  • 44

2 Answers2

1

You dont need the square brackets, get rid of them.

return jsonify(link=a["link"], title=a["title"])
             # ^At this point                 ^ and this one.

Read about keyword arguments in python.

Community
  • 1
  • 1
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
1

I think your dict syntax is wrong. You can read about more in official documentation.

The code that I think you are trying for is as follows:

@app.route("/articles")
def articles():
    a = lookup(33496)
    return jsonify({"link" : a["link"], "title" : a["title"]})

Specifically you should use curly braces instead of brackets ({}) and colon (:) instead of equals sign.

Another option is to let jsonify() to do the conversion (as pointed out in the other answer):

@app.route("/articles")
def articles():
    a = lookup(33496)
    return jsonify(link = a["link"], title = a["title"])

Nevertheless, I think you would be well advised to use create a dict. It becomes more flexible when you need to create larger JSON objects.

Hope this helps.

Jari
  • 38
  • 1
  • 6
  • what's the purpose of jsonify then, if you need to explicitly do the conversion to json? – tadm123 Feb 17 '17 at 03:41
  • Yea @Jari, the problem is that I already edited it the way that you pointed out using `jsonify()` but I'm getting some syntax errors. – tadm123 Feb 17 '17 at 03:43
  • I now tried using your first example and I'm getting the errors too. Hmm – tadm123 Feb 17 '17 at 03:46
  • Also, the point of jsonify() is to have a standard way of converting different content (be it dict, array or arguments) to JSON format that Flask can then return over HTTP. – Jari Feb 17 '17 at 03:55
  • Actually it's working now, I had some misspellings. Thanks – tadm123 Feb 17 '17 at 03:58