简体   繁体   中英

python error while looping from a list json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I'm making a script that fills a text document with responses from an api. The api is being asked to convert usernames from a list to universally unique identifiers. I keep getting this error and can't find a way around it. "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"

Sample of accounts.txt

knapplace
Coppinator
tynow
Pman59
ButterMusty
FlyHighGuy13
Seyashi
fluzzygirl1
SquidMan55
leonrules9
BarthGimble
MTR_30
Darkshadow402
Deathmyster
Team_Everlook
Sheathok
KCFrost
mendog
Allfaal117
theLP25D
Zimyx
Blurrnis
redboy678
moose_breeder
kaser12345
import requests
import json

file1 = open('accounts.txt', 'r')

usernames = []

for line in file1:

    stripped_line = line.strip()
    usernames.append(stripped_line)

file1.close()

for x in usernames:

    username = str(x)

    url = ("https://api.mojang.com/users/profiles/minecraft/"+username+"?at=1462770000")

    y = requests.get(url)
    y_data = y.json()
    uuid = y_data['id']

    uuids = []
    uuids.append(uuid)

    file2 = open('uuids.txt', 'w')
    file2.writelines(uuids)
    file2.close()

    file2 = open('uuids.txt', 'r')
    lines = file2.readlines()

I checked the URL you pasted. If the user does not exist, the API does not return any content but still returns a successful status. That is what the error means — it expected there to be a JSON object starting at char 0 .

Essentially, you need to handle the case when the response is empty before you try to execute a y.json() by checking y.content . If y.content is empty, skip processing the current username and go to the next one.

y = requests.get(url)
if len(y.content) == 0:
   continue  # Skip processing this username

# The rest of the code only runs if y.content is not empty.
y_data = y.json()
uuid = y_data['id']

Note: @Ali makes a great point about checking for an empty reply. With that fix it works like a champ for me with a few other minor changes:

  • Used usernames provided by OP instead of reading them in from a file.
  • Moved initialization of uuids out of for loop to avoid it being reset for each username.
  • Modfied file i/o stuff to what I am more used to working with. ;^)
import requests
import json

usernames = [
    "knapplace",
    "Coppinator",
    "tynow",
]

uuids = []
for x in usernames:

    username = str(x)

    url = ("https://api.mojang.com/users/profiles/minecraft/"+username+"?at=1462770000")

    y = requests.get(url)
    if len(y.content) == 0:
        continue  # Skip processing this username

    y_data = y.json()
    uuid = y_data['id']

    uuids.append(uuid)

with open('uuids.txt', 'w') as f:
    for uuid in uuids:
        f.write(uuid + '\n')

with open('uuids.txt', 'r') as f:
    read_data = f.read()

print(read_data)

Output:

c9998bafea3146d5935f4e215b6b4351
5c321f81409847a0907c4b30c342217f
9f206def69bf407fbab6de7c9b70ff80

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM