7

I want to use flask to return JSON to the brower with or without simplejson (with appropriate headers) here is what I have so far for my flask application:

@app.route('/')
def hello_world():
    QUERY_URL="http://someappserver:9902/myjsonservlet"
    result = simplejson.load(urllib.urlopen(QUERY_URL))
    return result;

Assuming the JSON output returned is:

{"myapplication":{"system_memory":21026160640.0,"percent_memory":0.34,
"total_queue_memory":4744,"consumers":1,"messages_unacknowledged":0,
"total_messages":0,"connections":1}

When I visit the page http://localhost:5000 however, I get a Internal Server Error. What must I do with "result" to get it to display appropriately? Or is there some way I can tell it to return with json headers?

When I add a print statement to print the result I can see the JSON, but in the browser it gives me an Internal Server Error.

1 Answer 1

10
import requests
r = requests.get(QUERY_URL)
return r.json

#normal return
return jsonify(username=g.user.username,
               email=g.user.email,
               id=g.user.id)

jsonify is available in flask. Here is the docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.