Facing error while normalising the HttpRequests <class , str> type while using urllib.request

Viewed 33

I am using urllib.request for GET method.

The response type is -> HttpRequests <class , str>

Problem: pandas.json_normalize is not possible because of this error

TypeError: byte indices must be integers or slices, not str

Question: How can I normalize the response?

This is my code:

req = urllib.request.Request(url, headers=hdr)

req.get_method = lambda: 'GET'
response = urllib.request.urlopen(req)
    
print(response.getcode())
json_response=response.read())

### Normalize the response
pandas.json_normalize( data=json_response['results'], meta=['id', 'name'] )

and this is a part of my JSON:

{
    "results": [{
            "accountId": "d85b3704-60c9-474f-aecc-2886629c732e",
            "id": "69fb205b25",
            "partition": null,
            "externalId": null,
            "metadata": null,...
1 Answers

Try this, it works for me.

 response_read=(response.read())
 response: bytes = response_read
 data = json.loads(response)
 ###Conver the Json Response to a dataframe
 df = pd.json_normalize(data , 'results')
 ###choose useful columns
 df = df.loc[:, df.columns.isin(['id','name'])]
 print(df[:5])
 ###Save dataframe CSV file
 df.to_csv('out.csv')
Related