0
my_trends = api.GetTrendsWoeid(my_woe_id)
trends = json.loads(my_trends)

but I get error : raise

TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list

. I am using python 3.7. and python-twitter. What I am doing wrong?

1 Answers1

0

Debugging such errors is part of the fun - how to do it?

First step: use python to inspect the thing that makes problems:

print(type(my_trends))       # will tell you it is a list
print(*my_trends)            # prints all list elements wich tells you what they are

then look into the API for what you are using: twitter.api.Api.GetTrendsWoeid which would tell you, that:

documentation thats shows the used method returns a list

which fully explains why you get

TypeError: the JSON object must be str, bytes or bytearray, not list.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • thanks, you are right it is a list, so how to load it into json as understand that it only accept string, I tried to use my_trends = str(api.GetTrendsWoeid(my_woe_id)) to convert it to string but I get :raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1) – Mohammed Alkindy May 09 '20 at 07:32
  • @Mohamm Check what the items you get returned are - maybe they are strings that can be parsed. If not - you probably do not need to json at all because the API you are using does this already internally and returns fully fledged python objects. I havent used this API - so I cant say. Please research the stuff you are using. – Patrick Artner May 09 '20 at 07:41