Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:28:18+00:00 2026-05-31T00:28:18+00:00

Learning python for the first time and it’s I’m reading the JSON and decoding

  • 0

Learning python for the first time and it’s I’m reading the JSON and decoding it correctly. I can loop through the names with the first for loop so I figured I was getting the value of names correctly. When I try to set that value to a variable I get the following error.

Traceback (most recent call last):
  File "./logger.py", line 70, in <module>
    winnerName = jsonData['players'][winner].get('name')
TypeError: list indices must be integers, not unicode

Python code:

jsonData = json.load(open('file.json'))

## Functions correctly and prints each name out.
for i in range(0,7):
  print jsonData['players'][i].get('name')

gameMatches = jsonData['games'][game]['matches']
for match in gameMatches:
  winnerBool = 1
  winner = -1
  loser = -1
  winnerName = ""
  loserName = ""
  matchCounter = 1

  ## Set the winner first then check to see who lost
  for key, value in sorted(match.iteritems(), reverse=True):
    if winnerBool:
      winner = value
  ## This is where the error keeps pointing
      winnerName = jsonData['players'][winner].get('name')
      winnerBool = 0
    elif winner != value:
      loserName = jsonData['players'][loser].get('name')
      loser = value

  print winnerName
  print loserName

JSON file:

{
   "players":[
      {"name":"Donatello","img":"\/img\/players\/1.jpg","rol":"\/img\/players\/r1.jpg"},
      {"name":"Leonardo","img":"\/img\/players\/2.jpg","rol":"\/img\/players\/r2.jpg"},
      {"name":"Michelangelo","img":"\/img\/players\/3.jpg","rol":"\/img\/players\/r3.jpg"},
      {"name":"Raphael","img":"img\/players\/4.jpg","rol":"img\/players\/r4.jpg"},
      {"name":"Shredder","img":"\/img\/players\/5.jpg","rol":"\/img\/players\/r5.jpg"},
      {"name":"Rocksteady","img":"\/img\/players\/6.jpg","rol":"\/img\/players\/r6.jpg"},
      {"name":"Bebop","img":"\/img\/players\/7.jpg","rol":"\/img\/players\/r7.jpg"},
      {"name":"Foot Soldier","img":"\/img\/players\/8.jpg","rol":"\/img\/players\/r8.jpg"}
      ],
   "games":[
      {"matches":[
         {"player1":"0","player2":"2","winner":"0"},
         {"player1":"1","player2":"3","winner":"3"},
         {"player1":"4","player2":"5","winner":"4"},
         {"player1":"6","player2":"7","winner":"6"},
         {"player1":"0","player2":"3","winner":"0"},
         {"player1":"4","player2":"6","winner":"4"},
         {"player1":"0","player2":"4","winner":"0"}
         ]
      },
      {"matches":[
         {"player1":"0","player2":"2","winner":"2"},
         {"player1":"1","player2":"3","winner":"3"},
         {"player1":"4","player2":"5","winner":"4"},
         {"player1":"6","player2":"7","winner":"7"},
         {"player1":"2","player2":"3","winner":"2"},
         {"player1":"4","player2":"7","winner":"4"},
         {"player1":"2","player2":"4","winner":"4"}
         ]
      }
   ]
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:28:19+00:00Added an answer on May 31, 2026 at 12:28 am

    Use

    winner = int(value)
    

    To convert from "1" (string) to 1 (integer)

    EDITED to clarify:

    when you do this….

    for key, value in sorted(match.iteritems(), reverse=True):
        #...
    

    …your key and value variables will be strings. I am assuming you are iterating over this part of your JSON:

    {"matches":[
         {"player1":"0","player2":"2","winner":"0"},
         {"player1":"1","player2":"3","winner":"3"},
    

    Therefore, when you assign…

    winner = value
    

    …and try to look up…

    winnerName = jsonData['players'][winner].get('name')
    

    …you get a…

    TypeError: list indices must be integers, not unicode
    

    …because you are saying:

    x = list["4"]
    

    where it should say:

    x = list[4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning python for the first time. I have an aim which is to
I'm learning Python and can't even write the first example: print 2 ** 100
I am new to programming and am learning Python as my first language. I
I am learning Python and building my first web app. I have been going
So, I started learning to code in Python and later Django . The first
I am learning Python and so far I can tell the things below about
I have a very basic problem. I am learning my first steps with python
I'm still learning python, and one of the first projects I decided to dive
I just started learning Python a couple of days ago as my first language
I just started learning Python (as my first language, so I know next to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.