0

I am a beginner at python. I am trying to use json.loads() in my code to iterate over a json object returned from an API.

The following is the error I am getting:

Traceback (most recent call last):
  File "/Users/Saket/Downloads/Telegram Desktop/update_trbble (2).py", line 209, in <module>
    today_trbbles=json.loads(open_discovery_api)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd8 in position 36: unexpected end of data
logout

I have checked out a few posts and tried to use .encode('utf-8') and also convert to unicode by using the unicode() method. These also are not working.

sample data:

status: 0,
message: "success",
results: {
totalTime: "3 ms",
queryTime: "0 ms",
status: 0,
cursor: "AoJ/spqIs9ICPwU4YzcyNWFlZi1hNjM1LTRiNzEtYjM1Ni02MWQ2MWFlMGQwZWU=",
numFound: 523,
size: 30,
songss: [
{
albumArt: "https://i.scdn.co/image/233728c2073337d67309fd205c6cc028e831d857",
artist: "Arty, Nadia Ali & BT",
docSource: [
"CACHE"
],
commentCount: 0,
createdBy: "wiredmau5",
createdTime: "Fri Jan 22 15:07:45 UTC 2016",
createdUTS: 1453475265411,
createdUserId: "ae186330-5fa5-469c-b1cc-8f3d3b61e538",
downVoteCount: 0,
favouriteCount: 0,
flag1Count: 0,
falg2Count: 0,
flag3Count: 0,
flag4Count: 0,
flag5Count: 0,
flag6Count: 0,
genre: [
"Trance"
],
genrePriority: 0,
hour: 0,
language: "english",
languagePriority: 0,
likeCount: 0,
minute: 0,
modifiedTime: {
time: 1453475748201,
minutes: 15,
seconds: 48,
hours: 15,
month: 0,
year: 116,
timezoneOffset: 0,
day: 5,
date: 22
},
saket.v
  • 173
  • 1
  • 9
  • Please include the data you're trying to decode, at least the relevant first ~40 bytes or so. – unwind Jan 22 '16 at 16:00
  • Sorry for the trouble. Added sample data – saket.v Jan 22 '16 at 16:04
  • Could you please provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)? That would help us a lot in trying to identify the problem that you are having. – Noctis Skytower Jan 22 '16 at 16:06
  • Possible duplicate of [UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c](http://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c) – Ulrich Eckhardt Jan 22 '16 at 18:45

1 Answers1

0

Your program is having an issue interpreting the encoding of the output of your API request. First, make sure you have both json and urllib.request imported. Then, you need to open the API URL, in this case, I set the output equal to byte_Obj. Then you will convert the information into ascii text using the .read() and .decode() function. Then, to make your indices integers you will use the .load() function from the json package you imported. Hope this helps!

    import json
    import urllib.request
    byte_Obj = urllib.request.urlopen("API URL Here") #urllib is used to open the URL of the API provided 
    json_string = byte_Obj.read().decode("ascii", "ignore") #to decode output into ascii text
    info = json.loads(json_string) #final info with indices as integers
Eliav Hamburger
  • 85
  • 2
  • 12