Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas read_json script that used to work now produces an error

I have a script that up until recently worked fine, but is now producing an error.

import requests
import pandas as pd

# Set the url to given endpoint
url = "https://SomeURL/SomeEndpoint"
print('URL set')

# Connect to endpoint with credentials and put results in dictionary
URLresponse = requests.get(url,auth=("SomeUser", "SomePassword"), verify=True)
print('connection to endpoint')

# Load the response as proper JSON into a var
rawdata = (URLresponse.content)
print(type(rawdata))
print('populating variable')

# print(rawdata)

# Load the var into a dataframe
df = pd.read_json(rawdata) 
print('load variable into df')

print(df)

This used to work fine but now it is producing an error as following:

  File "C:\Program Files\Python310\lib\site-packages\pandas\io\common.py", line 901, in get_handle
    raise TypeError(
TypeError: Expected file path name or file-like object, got <class 'bytes'> type

How can I go ahead to troubleshoot this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can change df = pd.read_json(rawdata) to df = pd.read_json(io.StringIO(rawdata.decode('utf-8')))

You will need to include import io earlier in your file as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading