I'm using Tweepy to download tweets. I have a program that then writes the actual Status
object to a file in text form. How do I translate this into JSON, or import this object back into Python? I've tried using the JSON library to encode, but Status is not JSON serializable.
Asked
Active
Viewed 4.5k times
48
-
1I did, I've tried using the JSON Library, but the object isn't JSON serializable – KOM Jan 12 '15 at 11:06
4 Answers
105
The Status
object of tweepy itself is not JSON serializable, but it has a _json
property which contains JSON serializable response data. For example:
>>> status_list = api.user_timeline(user_handler)
>>> status = status_list[0]
>>> json_str = json.dumps(status._json)

taskinoor
- 45,586
- 12
- 116
- 142
-
4Note that this is a private attribute set on the [model](https://github.com/tweepy/tweepy/blob/cd46550b3ef068857f5de9c37bbdd0a72acb7462/tweepy/models.py#L78) and not mentioned anywhere in the docs, nor used elsewhere in the code. So it may conceivably be removed one day. But on the other hand, as long as it's there, you can just as easily use it to create json dumps of the whole list using `json.dumps([status._json for status in status_list])`. – yoniLavi Nov 13 '16 at 02:56
-
2@yoniLavi yes, it wasn't mentioned in the doc and it was hard to find. On the other hand this is very useful. One of my teammates figured it out while we were trying to dump raw data on files. Hopefully tweepy developers will add this or something equivalent as documented method in future. – taskinoor Nov 13 '16 at 03:59
12
A better way to do this is to use a tweepy parser. It's not documented very well - see the Tweepy API reference - but it's a public API, so much safer than using the _json
property.
import tweepy
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
status = api.user_timeline(user=username, count=1)[0]
json.dumps(status)
status
is now a json object.

Greg
- 9,963
- 5
- 43
- 46
-
1
-
1I tried: status = `api.get_status(tweet_id)` and it gives me: `(
) is not JSON serializable` Try it yourself, if you can – salvob Jun 10 '18 at 22:36 -
but it should be a Status Object, so I think Status Object are not JSON serializable. – salvob Jun 11 '18 at 08:48
-
it gives me the same error for the example you posted, so even for user_timeline – salvob Jun 11 '18 at 09:06
-
2
1
users = api.search_users('TimHortons', 1)
print(json.dumps(users[0]._json))
Use json.dumps(users[0]._json)
if object has _json. Users was only an example.

Belloz
- 423
- 5
- 9
0
With the latest versions of tweepy, this became much easier. You only need to pass return_type=dict
when you construct your client:
client_v2 = tweepy.Client(
bearer_token="",
consumer_key="", # api key in dev page
consumer_secret="", # api key secret in dev page
access_token="", # for user
access_token_secret="", # for user
return_type=dict, # <<----- not mentioned in examples
)
response = client_v2.SOME_METHOD(...)
print(json.dumps(response, indent=4))

pembeci
- 696
- 4
- 9