Displaying JSON data to stackOverflow data using Flask

I was trying to display my username and reputation from JSON data retrieved from StackOverflow API.

Im using python module Queries to retrieve data. Here is the code

from flask import Flask,jsonify
import requests
import simplejson 
import json

app = Flask(__name__)

@app.route("/")
def home():
    uri = "https://api.stackexchange.com/2.0/users?   order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
    try:
        uResponse = requests.get(uri)
    except requests.ConnectionError:
       return "Connection Error"  
    Jresponse = uResponse.text
    return Jresponse

if __name__ == "__main__":
    app.run(debug = True)

      

Unused imports are what I need to do this, but don't seem to know how to do it. Below is what is returned to the browser, I just want to display username [display_name] and reputation. what options should i do to do this?

{ "": [{ "user_id": 540028, "user_type": "", "CREATION_DATE": 1292207782, "DISPLAY_NAME": "Fuchida", "profile_image": "HTTP://www.gravatar.com//6842025a595825e2de75dfc3058f0bee = identicon & = ", "?": 13, "reputation_change_day": 0, "reputation_change_week": 0, "reputation_change_month": 0, "reputation_change_quarter": 0, "reputation_change_year": 0, "": 24, "last_access_date": 1332905685, "LAST_MODIFIED_DATE": 1332302766, "is_employee" "": "http://stackoverflow.com/users/540028/fuchida", "WEBSITE_URL": "http://blog.Fuchida.me", "": " MN", "account_id": 258084, "badge_counts": { "": 0, "": 0, "": 3}}], "quota_remaining": 282, "quota_max": 300, "has_more" }

+3


source to share


1 answer


Use json.loads () to read and decode data.



from flask import Flask,jsonify
import requests
import simplejson 
import json

app = Flask(__name__)

@app.route("/")
def home():
    uri = "https://api.stackexchange.com/2.0/users?   order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
    try:
        uResponse = requests.get(uri)
    except requests.ConnectionError:
       return "Connection Error"  
    Jresponse = uResponse.text
    data = json.loads(Jresponse)

    displayName = data['items'][0]['display_name']# <-- The display name
    reputation = data['items'][0]['reputation']# <-- The reputation

    return Jresponse

if __name__ == "__main__":
    app.run(debug = True)

      

+4


source







All Articles