3

I've been trying to display my username and reputation from the JSON data retrieved from the StackOverflow API.

Im using the python module Requests to retrieve the 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)

The unused imports is what I need to get this done but cant seem to know how to get it done. Below is what is returned to the browser, I want to just display the username [display_name] and the reputation. what options do I have to get this done ?

{"items":[{"user_id":540028,"user_type":"registered","creation_date":1292207782,"display_name":"Fuchida","profile_image":"http://www.gravatar.com/avatar/6842025a595825e2de75dfc3058f0bee?d=identicon&r=PG","reputation":13,"reputation_change_day":0,"reputation_change_week":0,"reputation_change_month":0,"reputation_change_quarter":0,"reputation_change_year":0,"age":24,"last_access_date":1332905685,"last_modified_date":1332302766,"is_employee":false,"link":"http://stackoverflow.com/users/540028/fuchida","website_url":"http://blog.Fuchida.me","location":"Minneapolis MN","account_id":258084,"badge_counts":{"gold":0,"silver":0,"bronze":3}}],"quota_remaining":282,"quota_max":300,"has_more":false}

1 Answer 1

4

Use json.loads() to read and decode the 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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! This resolved the issue for me be it with the below adjustment. It only returns the username but that good enough for now. Thanks ! This resolved the issue for me be it with the below adjustment. It only returns the username but that good enough for now.""""" displayName = parsedData['items'][0]['display_name'] return displayName

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.