2

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()
2
  • Can you provide a list of sample user names that would allow us to try and debug your code? Since we don't have the accounts.txt file it is hard to see what is going on just by looking at your code. Commented Feb 18, 2021 at 19:51
  • I have added a sample for you Commented Feb 18, 2021 at 20:18

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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']

7 Comments

how would I check y.content? a simple if y.content != None?
y.content is a byte string. You would could check if y.content == b'' or len(y.content) == 0.
it returned one "uuid" and returned the same error?
I added a sample code snippet to my answer. Are you doing something similar to what I posted and still receiving the error?
final issue, the script is working as intended except for the fact it is replacing the previous uuid
|

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.