简体   繁体   中英

Python get data with JSON response

I'm making a call to an api which is returning a JSON response, whcih i am then trying to retrieve certain data from within the response.

{'data': {'9674': {'category': 'token',
                   'contract_address': [{'contract_address': '0x2a3bff78b79a009976eea096a51a948a3dc00e34',
                                         'platform': {'coin': {'id': '1027',
                                                               'name': 'Ethereum',
                                                               'slug': 'ethereum',
                                                               'symbol': 'ETH'},
                                                      'name': 'Ethereum'}}],
                   'date_added': '2021-05-10T00:00:00.000Z',
                   'date_launched': '2021-05-10T00:00:00.000Z',
                   'description': 'Wilder World (WILD) is a cryptocurrency '
                                  'launched in 2021and operates on the '
                                  'Ethereum platform. Wilder World has a '
                                  'current supply of 500,000,000 with '
                                  '83,683,300.17 in circulation. The last '
                                  'known price of Wilder World is 2.28165159 '
                                  'USD and is down -6.79 over the last 24 '
                                  'hours. It is currently trading on 21 active '
                                  'market(s) with $2,851,332.76 traded over '
                                  'the last 24 hours. More information can be '
                                  'found at https://www.wilderworld.com/.',
                   'id': 9674,
                   'is_hidden': 0,
                   'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/9674.png',
                   'name': 'Wilder World',
                   'notice': '',
                   'platform': {'id': 1027,
                                'name': 'Ethereum',
                                'slug': 'ethereum',
                                'symbol': 'ETH',
                                'token_address': '0x2a3bff78b79a009976eea096a51a948a3dc00e34'},
                   'self_reported_circulating_supply': 19000000,
                   'self_reported_tags': None,
                   'slug': 'wilder-world',
                   'subreddit': '',
                   'symbol': 'WILD',
                   'tag-groups': ['INDUSTRY',
                                  'CATEGORY',
                                  'INDUSTRY',
                                  'CATEGORY',
                                  'CATEGORY',
                                  'CATEGORY',
                                  'CATEGORY'],
                   'tag-names': ['VR/AR',
                                 'Collectibles & NFTs',
                                 'Gaming',
                                 'Metaverse',
                                 'Polkastarter',
                                 'Animoca Brands Portfolio',
                                 'SkyVision Capital Portfolio'],
                   'tags': ['vr-ar',
                            'collectibles-nfts',
                            'gaming',
                            'metaverse',
                            'polkastarter',
                            'animoca-brands-portfolio',
                            'skyvision-capital-portfolio'],
                   'twitter_username': 'WilderWorld',
                   'urls': {'announcement': [],
                            'chat': [],
                            'explorer': ['https://etherscan.io/token/0x2a3bff78b79a009976eea096a51a948a3dc00e34'],
                            'facebook': [],
                            'message_board': ['https://medium.com/@WilderWorld'],
                            'reddit': [],
                            'source_code': [],
                            'technical_doc': [],
                            'twitter': ['https://twitter.com/WilderWorld'],
                            'website': ['https://www.wilderworld.com/']}}},
 'status': {'credit_count': 1,
            'elapsed': 7,
            'error_code': 0,
            'error_message': None,
            'notice': None,
            'timestamp': '2022-01-20T21:33:04.832Z'}}

The data i am trying to get is 'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/9674.png', but this sits within [data][9674][logo]

But as this script to running in the background for other objects, i won't know what the number [9674] is for other requests.

So is there a way to get that number automatically?

[data] will always be consistent.

Im using this to get the data back

session = Session()
session.headers.update(headers)

response = session.get(url, params=parameters)

pprint.pprint(json.loads(response.text)['data']['9674']['logo'])

You can try this:

session = Session()
session.headers.update(headers)

response = session.get(url, params=parameters)

resp = json.loads(response.text)

pprint.pprint(resp['data'][next(iter(resp['data']))]['logo'])

where next(iter(resp['data'])) - returns first key in resp['data'] dict. In your example it '9674'

With .keys() you get a List of all Keys in a Dictionary.

So you can use keys = json.loads(response.text)['data'].keys() to get the keys in the data-dict.

If you know there is always only one entry in 'data' you could use json.loads(response.text)['data'][keys[0]]['logo'] . Otherwise you would need to iterate over all keys in the list and check which one you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM