0

I am new to Python and I am having trouble collecting data from this json file using list comprehension but it is not working so far, so how I structure this list? I need to collect the tagName element of each skill.

This is what I tried:

   def getUserSkills(handleList): #List of Strings
     for handles in handleList:
     response1 = requests.get("http://api.topcoder.com/v3/members/" +  handles + "/skills")
     data = response1.json()

     skillList = [skill['tagName'] for skill in data['result']['content']['skills']]

     print(skillList)

Json File:

"id":"-462bfb3:16a2448d765:4ed3",
"result":{
  "success":true,
  "status":200,
  "metadata":null,
  "content":{
     "userId":21932422,
     "userHandle":"saarixx",
     "handleLower":"saarixx",
     "skills":{
        "130":{
           "tagName":"Brute Force",
           "hidden":false,
           "score":88.0,
           "sources":[
              "CHALLENGE"
           ]
        },
        "259":{
           "tagName":"JSON",
           "hidden":false,
           "score":5.0,
           "sources":[
              "CHALLENGE"
           ]
        },
1
  • 1
    What is not working? What is your input and output? read this Commented Apr 16, 2019 at 16:25

2 Answers 2

1

Iterate through the dictionary given by dct['result']['content']['skills'] and capture value['tagName]

dct = {
    "id": "-462bfb3:16a2448d765:4ed3",
    "result": {
        "success": True,
        "status": 200,
        "metadata": None,
        "content": {
            "userId": 21932422,
            "userHandle": "saarixx",
            "handleLower": "saarixx",
            "skills": {
                "130": {
                    "tagName": "Brute Force",
                    "hidden": False,
                    "score": 88.0,
                    "sources": [
                        "CHALLENGE"
                    ]
                },
                "259": {
                    "tagName": "JSON",
                    "hidden": False,
                    "score": 5.0,
                    "sources": [
                        "CHALLENGE"
                    ]
                }
            }
        }
    }
}

skillList = [value['tagName'] for key,value in dct['result']['content']['skills'].items()]
print(skillList)
#['Brute Force', 'JSON']
Sign up to request clarification or add additional context in comments.

2 Comments

good, but what if I wanted to collect more than one data at a time like score and tagName?
Can you update that example in your question, and I can take a look
0
import requests


def getUserSkills(handleList):  # List of Strings
    data = []
    for handles in handleList:
        response = requests.get("http://api.topcoder.com/v3/members/" + handles + "/skills")
        data.append(response.json())
    skillList = [skill['tagName'] for skill in data['result']['content']['skills']]

    print(skillList)


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.