Convert tweepy JSON object to dict

I would like to convert the result of calling Tweedy api.trends_location(woeid)to dict (or dict of dicts), so I can work with the values ​​(in fact, I want to end with dict of 'name "). The Tweepy documentation says that the result is a" JSON object "( see here. ), but when I extract it, type(retrieved)estimated as listof course. retrievedis lenof 1, and retrieved[0]gives me one element
 [{'trends': [{'url': 'http://search.twitter.com/search?q=%23questionsidontlike', 'query': '%23questionsidontlike', 'events': None, 'promoted_content': None, 'name': '#questionsidontlike'}, ], (more of the same), 'created_at': '2011-01-31T22:39:16Z', 'as_of': '2011-01-31T22:47:47Z', 'locations': [{'woeid': 23424977, 'name': 'United States'}]}].

I can call json.dumpswhich will give a well formatted view, but this is not very useful for me, but json.loadsgives me:__init__() got an unexpected keyword argument 'sort_keys'

How should I proceed?

Link to the full code: https://gist.github.com/805129

+3
source share
4 answers

Well, that should do it! It is even verified (thanks for sending more information).

>>> names = [trend["name"] for trend in retrieved[0]["trends"]]
>>> names
['#wishuwould', '#questionsidontlike', '#februarywish', 'Purp & Patron', 'Egyptians', 'Kool Herc', 'American Pie', 'Judge Vinson', 'Eureka Nutt', 'Eddie House']

I think that most of the confusion came from documentation referring to the result as a JSON object, which is different from a JSON string that needs to be converted using a module json.

How it works: retrieved- this is a list containing one element, which is a dictionary containing a key trends, therefore retrieved[0]["trends"]- this is a list of trend dictionaries, where each trend dictionary contains namewhich you are interested in.

+2
source

Something like this work for you?

def searchKeys(struct, keys, result = None, recursive = True):
        if result is None:
                result = []

        if isinstance(struct, dict):
                for k in keys:
                        if struct.has_key(k):
                                result.append(struct[k])

                if recursive:
                        for i in struct.values():
                                searchKeys(struct = i, keys = keys, result = result, recursive = recursive)
        elif isinstance(struct, list):
                if recursive:
                        for i in struct:
                                searchKeys(struct = i, keys = keys, result = result, recursive = recursive)

        return result

Usage example:

>>> searchKeys(struct = a, keys = ['name'])
['United States', '#questionsidontlike']

dict/list, dict list.

+2

Tweepy 'Status' Python (JSON), "_json" .

tweets = tweepy_api.user_timeline(screen_name='seanharr11')
json_tweets = map(lambda t: t._json, tweets)
+1
source
>>> import simplejson
>>> a = {"response":[{"message":"ok"},{"message":"fail"}]}
>>> json = simplejson.dumps(a)
>>> simplejson.loads(json)
{'response': [{'message': 'ok'}, {'message': 'fail'}]}

http://docs.python.org/library/json.html

0
source

Source: https://habr.com/ru/post/1789116/


All Articles