Python not decoding json
Question: I am receiving the error:

    
    
    No JSON object could be decoded: line 1 column 0 (char 0)
    

when trying to parse a json object returned from the xbox music api.

Here is my code:

    
    
    conn = httplib.HTTPSConnection(SERVICE_API)
    conn.request("GET", url)
    response = conn.getresponse()
    data = response.read()
    j = json.loads(data)
    

the last line is what returned the above error. If I comment that out and
print out 'data', I can see the json object. If i copy / past that to
jsonlint.com to see if it's valid, it says that it is. I would post it here
but it is 800 lines.


Answer: You are accessing a Microsoft service that includes the UTF-8 BOM at the
start. But Microsoft is rather fond of the BOM in UTF-8 (which is entirely
redundant here) as it introduced the BOM to UTF-8 to make it easier for
Notepad and other Windows applications to auto-detect the encoding used.

Silly Microsoft, that is very much _not valid_ according to the JSON RFC; if
there is a contact address anywhere you should report this as a bug to them.

You can strip the BOM with:

    
    
    import codecs
    
    data = response.read()
    if data.startswith(codecs.BOM_UTF8):
        data = data[3:]
    

