'I am stuck on trying to load JSON data from URL into dataframe

I think my code sample is fairly close, but I'm getting this error message.

urllib.request.urlretrieve(df,DOWNLOAD_PATH)
Traceback (most recent call last):

  File "C:\Users\ryans\AppData\Local\Temp\ipykernel_25952\2076090426.py", line 1, in <module>
    urllib.request.urlretrieve(df,DOWNLOAD_PATH)

  File "C:\Users\ryans\anaconda3\lib\urllib\request.py", line 245, in urlretrieve
    url_type, path = _splittype(url)

  File "C:\Users\ryans\anaconda3\lib\urllib\parse.py", line 1019, in _splittype
    match = _typeprog.match(url)

TypeError: expected string or bytes-like object

The error seems to occur on this line of code: urllib.request.urlretrieve(df,DOWNLOAD_PATH)

Here is the code that I am testing.

import urllib
#from urllib.request import urlopen as uReq
#from urllib.request import urlopen
import requests
import json
import pandas as pd
from pandas.io.json import json_normalize

all_links = ['https://www.baptisthealthsystem.com/docs/global/standard-charges/474131755_abrazomaranahospital_standardcharges.json?sfvrsn=9a27928_2',
  'https://www.baptisthealthsystem.com/docs/global/standard-charges/621861138_abrazocavecreekhospital_standardcharges.json?sfvrsn=674fd6f_2',
  'https://www.baptisthealthsystem.com/docs/global/standard-charges/621809851_abrazomesahospital_standardcharges.json?sfvrsn=13953222_2',
  'https://www.baptisthealthsystem.com/docs/global/standard-charges/621811285_abrazosurprisehospital_standardcharges.json?sfvrsn=c8113dcf_2']
for item in all_links:
    #print(item)
    try:
        length = len(item)
        first_under = item.find('_') + 1
        last_under = item.rfind('?') - 21
        file_name = item[first_under:last_under]
        r = requests.get(item)
        print(r.json)
        df = pd.DataFrame(r)
        df.head()
        DOWNLOAD_PATH = 'C:\\Users\\ryans\\Desktop\\hospital_data\\' + file_name + '.csv'
        urllib.request.urlretrieve(df,DOWNLOAD_PATH)
    except Exception as e: print(e)

I think, maybe, the JSON is not being parsed correctly, but I can't tell for sure what the issue is.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source