Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'])

>Solution :

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'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading