Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Downloading or reading a JSON file from OneDrive

Open anousias-k3y opened this issue 9 months ago • 2 comments

I was working on a module for programmatically handling files from an onedrive folder and I get a weird JSON decode error specifically when downloading JSON files or in general attempting to get their content:

Expecting value: line 8 column 24 (char 216)
simplejson.errors.JSONDecodeError: Expecting value: line 8 column 24 (char 216)

It seems that, for some reason, before downloading, the client attempts to decode the file first (?).

I am following the corresponding example for downloading files:

drive_items = remote_folder.children.get_all().execute_query()
    
for drive_item in drive_items:

    if drive_item.file is None:
        continue
        
    # download file content
    local_path = os.path.join(local_folder, drive_item.name)

    with open(local_path, "wb") as local_file:
        drive_item.download(local_file).execute_query_retry()

I don't know if this is the same kind of error with #598 where I first commented. As a commenter mentioned there, if I manually rename the file with a different suffix, e.g. .jsn I can download the file with no problem.

Also I need to mention that the .json decode error probably originates due to some nan values encoded as NaN (default behavior of the python json library) which may raise an error with other libraries (simplejson) which are strict with rfc7159(?) and require to pass an argument such as allow_nan=True. Whatever the case, having to decode a .json file before downloading it, seems to me pretty irrelevant.

versions:

  • python==3.10
  • office365-rest-python-client==2.5.11

anousias-k3y avatar Feb 27 '25 09:02 anousias-k3y

This has caught me out too... I have a wrapper function for downloading file content as bytes, but it gets caught out because Office365-rest-python-client decides to decode some files itself. I'm still trying to find a way to avoid this behaviour.

adamgreg avatar Aug 14 '25 11:08 adamgreg

I think this might be a duplicate of a very old issue: #598 . There's a workaround described over there,

adamgreg avatar Aug 14 '25 11:08 adamgreg