Related
I am integrating Paypal in django-rest- framework. The issue is with paypal webhook response. I get a webhook json response when ever I pay some amount on paypal already created a url path in my application for the same.
Sample JSON object response:
{'event_version': '1.0', 'create_time': '2022-12-19T18:57:12.343Z', 'resource_type': 'checkout-order', 'resource_version': '2.0', 'event_type': 'CHECKOUT.ORDER.APPROVED', 'summary': 'An order has been approved by buyer', 'resource': {'update_time': '2022-12-19T18:57:02Z', 'create_time': '2022-12-19T18:56:51Z', 'purchase_units': [{'reference_id': 'default', 'amount': {'currency_code': 'USD', 'value': '10.00'}, 'payee': {'email_address': 'mailto:sb-dgfcl23459178#business.example.com', 'merchant_id': ''jkdshfhsjdfhjksfhjsfs'}, 'custom_id': 'e-book-1234', 'shipping': {'name': {'full_name': 'John Doe'}, 'address': {'address_line_1': '1 Main St', 'admin_area_2': 'San Jose', 'admin_area_1': 'CA', 'postal_code': '95131', 'country_code': 'US'}}, 'payments': {'captures': [{'id': '9HW93194EE464044X', 'status': 'COMPLETED', 'amount': {'currency_code': 'USD', 'value': '10.00'}, 'final_capture': True, 'seller_protection': {'status': 'ELIGIBLE', 'dispute_categories': ['ITEM_NOT_RECEIVED', 'UNAUTHORIZED_TRANSACTION']}, 'seller_receivable_breakdown': {'gross_amount': {'currency_code': 'USD', 'value': '10.00'}, 'paypal_fee': {'currency_code': 'USD', 'value': '0.84'}, 'net_amount': {'currency_code': 'USD', 'value': '9.16'}}, 'links': [{'href': 'https://api.sandbox.paypal.com/v2/payments/captures/9HW93194EE464044X', 'rel': 'self', 'method': 'GET'}, {'href': 'https://api.sandbox.paypal.com/v2/payments/captures/9HW93194EE464044X/refund', 'rel': 'refund', 'method': 'POST'}, {'href': 'https://api.sandbox.paypal.com/v2/checkout/orders/4CV868937U646315D', 'rel': 'up', 'method': 'GET'}], 'create_time': '2022-12-19T18:57:02Z', 'update_time': '2022-12-19T18:57:02Z'}]}}], 'links': [{'href': 'https://api.sandbox.paypal.com/v2/checkout/orders/4CV868937U646315D', 'rel': 'self', 'method': 'GET'}], 'id': '4CV868937U646315D', 'payment_source': {'paypal': {}},
'intent': 'CAPTURE', 'payer': {'name': {'given_name': 'John', 'surname': 'Doe'},
'email_address': "adsfhjasdfhdajhk#gmail.com"
'payer_id': 'CSNQVZ49MMDA2',
'address': {'country_code': 'US'}},
'status': 'COMPLETED'},
'links': [{'href': 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-65K55150EC946744T-1T312384BP483891G', 'rel': 'self', 'method': 'GET'}, {'href': 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-65K55150EC946744T-1T312384BP483891G/resend', 'rel': 'resend', 'method': 'POST'}]}
Is there any inbuilt library which can handle this response in python or database design to handle this or I need to create it myself.
From a nested dictionary also containing lists I would like to return the values for specified keys whether the return value be another dictionary, list or singular value. Example dictionary shown below (OSINT API data of a random IP from Shodan).
From a specified list of keys e.g: ['domains', 'html', 'CN']
Example required output:
'domains': ['facebook.com']
'CN': '*.secure.facebook.com'
'html': ''
etc.
The specified keys may appear more than once, or not at all depending on the services on different IP's.
I have code for nested dictionaries but this has stumped me:
dataoutput = {'some': 'dictionary'}
filtered_list = ['list of required keys']
def seek_keys(d, key_list):
for k, v in d.items():
if k in key_list:
if isinstance(v, dict):
print(k + ": " + list(v.keys())[0])
else:
print(k + ": " + str(v))
if isinstance(v, dict):
seek_keys(v, key_list)
seek_keys(dataoutput, filtered_list)
{'region_code': '40', 'ip': 520966673, 'postal_code': None, 'country_code': 'JP', 'city': 'Tokyo', 'dma_code': None, 'last_update': '2021-08-17T21:19:17.704800', 'latitude': 35.6895, 'tags': [], 'area_code': None, 'country_name': 'Japan', 'hostnames': ['edge-secure-shv-01-nrt1.facebook.com'], 'org': 'Facebook Ireland Ltd', 'data': [{'hash': 960245092, '_shodan': {'id': '35fbfc68-de4a-4433-8d4e-672ed558dc90', 'options': {}, 'ptr': True, 'module': 'http', 'crawler': 'f4bb88763d8ed3a0f3f91439c2c62b77fb9e06f3'}, 'http': {'robots_hash': None, 'redirects': [], 'securitytxt': None, 'title': None, 'sitemap_hash': None, 'robots': None, 'server': None, 'host': '31.13.82.17', 'html': '', 'location': '/', 'html_hash': 0, 'sitemap': None, 'securitytxt_hash': None}, 'os': None, 'opts': {}, 'timestamp': '2021-08-17T21:19:17.704800', 'isp': 'Facebook, Inc.', 'port': 80, 'hostnames': ['edge-secure-shv-01-nrt1.facebook.com'], 'location': {'city': 'Tokyo', 'region_code': '40', 'area_code': None, 'longitude': 139.69171, 'country_code3': None, 'country_name': 'Japan', 'postal_code': None, 'dma_code': None, 'country_code': 'JP', 'latitude': 35.6895}, 'ip': 520966673, 'domains': ['facebook.com'], 'org': 'Facebook Ireland Ltd', 'data': 'HTTP/1.1 301 Moved Permanently\r\nVary: Accept-Encoding\r\nLocation: http://www.facebook.com/\r\nContent-Type: text/html; charset="utf-8"\r\nX-FB-Debug: uH7oyeyXmCxXRUEDRNZW89jYu4Ncis+tsOcmWtF45ENW8qkGHHJOHpF/WMOclN/XJahPWteD9avoPOcGo4g+Iw==\r\nDate: Tue, 17 Aug 2021 21:19:11 GMT\r\nAlt-Svc: h3-29=":443"; ma=3600,h3-27=":443"; ma=3600\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n', 'asn': 'AS32934', 'transport': 'tcp', 'ip_str': '31.13.82.17'}, {'hash': 1932904474, '_shodan': {'id': '5e465672-0345-4d13-94b4-f4b5a68e06dd', 'options': {}, 'ptr': True, 'module': 'https', 'crawler': 'bf213bc419cc8491376c12af31e32623c1b6f467'}, 'http': {'robots_hash': None, 'redirects': [], 'securitytxt': None, 'title': None, 'sitemap_hash': None, 'robots': None, 'server': None, 'host': '31.13.82.17', 'html': '', 'location': '/', 'html_hash': 0, 'sitemap': None, 'securitytxt_hash': None}, 'os': None, 'opts': {'vulns': [], 'heartbleed': '2021/08/15 19:43:47 31.13.82.17:443 - SAFE\n'}, 'timestamp': '2021-08-15T19:43:33.722160', 'isp': 'Facebook, Inc.', 'port': 443, 'ssl': {'chain_sha256': ['1e3839fdfad7b0a9098bfe4e2853391a6230357e50c6506e26caecdf85b95c9e', '19400be5b7a31fb733917700789d2f0a2471c0c9d506c0e504c06c16d7cb17c0', '7431e5f4c3c1ce4690774f0b61e05440883ba9a01ed00ba6abd7806ed3b118cf'], 'jarm': '29d29d00029d29d00041d43d00041d92bf66d3b7eed52dd4b99d709662a4a4', 'chain': ['-----BEGIN CERTIFICATE-----\nMIIG4jCCBcqgAwIBAgIQDNU+hEWn8AdrZCyl+p9BCjANBgkqhkiG9w0BAQsFADBw\nMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\nd3cuZGlnaWNlcnQuY29tMS8wLQYDVQQDEyZEaWdpQ2VydCBTSEEyIEhpZ2ggQXNz\ndXJhbmNlIFNlcnZlciBDQTAeFw0yMTA2MjcwMDAwMDBaFw0yMTA5MjUyMzU5NTla\nMHAxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRMwEQYDVQQHEwpN\nZW5sbyBQYXJrMRcwFQYDVQQKEw5GYWNlYm9vaywgSW5jLjEeMBwGA1UEAwwVKi5z\nZWN1cmUuZmFjZWJvb2suY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC\nAQEAvp5pfStLmHEZkL/h8EDLbYDAUI0A8P937l/Nn/sEPP+4qATFOpGK9r4xWl7V\nHBqv14a7FSR3Wcf17H5acZtEd2RPQXJqTjwzQ5sngQ0INFt3up1TrRyfavRJ4+jr\nVDPMeUF0nTfgBCkB0XvfSfrrTld9iYbsuINSLBRx8KGPsCqAM6AefPS/4YTXyPn+\nCZ7YBsZBbVhcQ7opioe/2yl/nxGuugxWHfqyj8wZOkLr44I9bB2lJodZbmGgHIel\nH0zetndOTS/0DuE1UW908B4cRJtnBTQ6GLi16BeW3HM1sRhtkvegfhpja72d0uJD\nVK6NJUC71KwPpI73Z69SYlRunQIDAQABo4IDdjCCA3IwHwYDVR0jBBgwFoAU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hoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\nnzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\neM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\nhS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\nYzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\nvEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n+OkuE6N36B9K\n-----END CERTIFICATE-----\n'], 'dhparams': None, 'versions': ['-TLSv1', '-SSLv2', '-SSLv3', '-TLSv1.1', 'TLSv1.2', 'TLSv1.3'], 'acceptable_cas': [], 'tlsext': [{'id': 65281, 'name': 'renegotiation_info'}, {'id': 11, 'name': 'ec_point_formats'}, {'id': 35, 'name': 'session_ticket'}], 'ja3s': 'ccc514751b175866924439bdbb5bba34', 'cert': {'sig_alg': 'sha256WithRSAEncryption', 'issued': '20210627000000Z', 'expires': '20210925235959Z', 'expired': False, 'version': 2, 'extensions': [{'data': '0\\x16\\x80\\x14Qh\\xff\\x90\\xaf\\x02\\x07u<\\xcc\\xd9edb\\xa2\\x12\\xb8Yr;', 'name': 'authorityKeyIdentifier'}, {'data': '\\x04\\x14A\\xdc\\xeaH-\\xcb\\xb2\\xf8J\\xca\\xf4v{C\\r\\x82\\x99\\xc6\\xfbq', 'name': 'subjectKeyIdentifier'}, {'data': '0,\\x82\\x15*.secure.facebook.com\\x82\\x13secure.facebook.com', 'name': 'subjectAltName'}, {'critical': True, 'data': '\\x03\\x02\\x05\\xa0', 'name': 'keyUsage'}, {'data': '0\\x14\\x06\\x08+\\x06\\x01\\x05\\x05\\x07\\x03\\x01\\x06\\x08+\\x06\\x01\\x05\\x05\\x07\\x03\\x02', 'name': 'extendedKeyUsage'}, {'data': '0l04\\xa02\\xa00\\x86.http://crl3.digicert.com/sha2-ha-server-g6.crl04\\xa02\\xa00\\x86.http://crl4.digicert.com/sha2-ha-server-g6.crl', 'name': 'crlDistributionPoints'}, {'data': "0503\\x06\\x06g\\x81\\x0c\\x01\\x02\\x020)0\\'\\x06\\x08+\\x06\\x01\\x05\\x05\\x07\\x02\\x01\\x16\\x1bhttp://www.digicert.com/CPS", 'name': 'certificatePolicies'}, {'data': '0u0$\\x06\\x08+\\x06\\x01\\x05\\x05\\x070\\x01\\x86\\x18http://ocsp.digicert.com0M\\x06\\x08+\\x06\\x01\\x05\\x05\\x070\\x02\\x86Ahttp://cacerts.digicert.com/DigiCertSHA2HighAssuranceServerCA.crt', 'name': 'authorityInfoAccess'}, {'critical': True, 'data': '0\\x00', 'name': 'basicConstraints'}, {'data': '\\x04\\x82\\x01i\\x01g\\x00u\\x00\\xf6\\\\\\x94/\\xd1w0"\\x14T\\x18\\x080\\x94V\\x8e\\xe3M\\x13\\x193\\xbf\\xdf\\x0c/ \\x0b\\xccN\\xf1d\\xe3\\x00\\x00\\x01zN\\x94\\x82>\\x00\\x00\\x04\\x03\\x00F0D\\x02 X\\x84h\\xb6\\xc8\\x10F\\xed\\xcc\\xe8x\\\\P\\xc9;\\xbc\\xcf\\x1cO\\x82\\xa5\\x08u\\xc9\\x96im\\xe1\\xc3a\\x10\\xf3\\x02 \\x02\\\'d\\xe4t2h\\xf9`\\xab\\x1d/\\xf6\\xf8\\x14\\x95\\xdfJ\\xf2\\xdf?\\xdc\\x9d\\xbd5\\xfcb\\xc8Z\\x07\\xb1\\xc2\\x00w\\x00\\\\\\xdcC\\x92\\xfe\\xe6\\xabED\\xb1^\\x9a\\xd4V\\xe6\\x107\\xfb\\xd5\\xfaG\\xdc\\xa1s\\x94\\xb2^\\xe6\\xf6\\xc7\\x0e\\xca\\x00\\x00\\x01zN\\x94\\x82P\\x00\\x00\\x04\\x03\\x00H0F\\x02!\\x00\\xd2\\xb52\\x13\\xdb\\x05\\x90=AT\\xee\\xca\\xfc\\xa0H.\\xa3+\\x9f\\x88h\\xfc\\xab\\x9c=EP^\\x1c\\x7f\\xc0^\\x02!\\x00\\xeb\\x16\\x13\\x7f\\x88\\xd3\\xa2\\xd0\\xc4E\\xc8\\x161<X\\x9c\\xff\\x85\\xd5i\\xcd\\x07E(\\x90Z\\xa7\\xfb=|6\\xd1\\x00u\\x00\\xee\\xc0\\x95\\xee\\x8drd\\x0f\\x92\\xe3\\xc3\\xb9\\x1b\\xc7\\x12\\xa3ij\\t{Kj\\x1a\\x148\\xe6G\\xb2\\xcb\\xed\\xc5\\xf9\\x00\\x00\\x01zN\\x94\\x82\\x83\\x00\\x00\\x04\\x03\\x00F0D\\x02 /\\x8a~\\xec\\xea\\xf8cjb\\xd4\\xb9\\x01y\\xa5\\x97\\x8b\\xa7J\\x94tO\\x05\\x11\\x1dX\\x1f\\xf5#v\\xd9$&\\x02 .t\\xdb\\xe3\\xe6\\x15\\xed\\x1c\\xf6\\x1c\\xba#Z]\\xb2\\xa3\\x88\\x8c\\xfb\\x0b\\x12\\xde\\x10\\xc7b)\\xaf{\\t\\xe7\\xe3\\xf9', 'name': 'ct_precert_scts'}], 'fingerprint': {'sha256': '1e3839fdfad7b0a9098bfe4e2853391a6230357e50c6506e26caecdf85b95c9e', 'sha1': '73f915b5c8218ff445be1de566b5c2d0a15d1f6d'}, 'serial': 17057963169357276840551436714988486922, 'subject': {'C': 'US', 'L': 'Menlo Park', 'CN': '*.secure.facebook.com', 'O': 'Facebook, Inc.', 'ST': 'California'}, 'pubkey': {'type': 'rsa', 'bits': 2048}, 'issuer': {'C': 'US', 'OU': 'www.digicert.com', 'O': 'DigiCert Inc', 'CN': 'DigiCert SHA2 High Assurance Server CA'}}, 'cipher': {'version': 'TLSv1/SSLv3', 'bits': 128, 'name': 'ECDHE-RSA-AES128-GCM-SHA256'}, 'trust': {'revoked': False, 'browser': {'mozilla': True, 'apple': True, 'microsoft': True}}, 'handshake_states': ['before/connect initialization', 'SSLv2/v3 write client hello', 'SSLv2/v3 read server hello', 'SSLv3/TLS read server hello', 'SSLv3/TLS read server certificate', 'SSLv3/TLS read server key exchange', 'SSLv3/TLS read server done', 'SSLv3/TLS write client key exchange', 'SSLv3/TLS write change cipher spec', 'SSLv3/TLS write finished', 'SSLv3/TLS flush data', 'SSLv3/TLS read server session ticket', 'SSLv3/TLS read finished', 'SSL negotiation finished successfully'], 'alpn': [], 'ocsp': {}}, 'hostnames': ['edge-secure-shv-01-nrt1.facebook.com'], 'location': {'city': 'Tokyo', 'region_code': '40', 'area_code': None, 'longitude': 139.69171, 'country_code3': None, 'country_name': 'Japan', 'postal_code': None, 'dma_code': None, 'country_code': 'JP', 'latitude': 35.6895}, 'ip': 520966673, 'domains': ['facebook.com'], 'org': 'Facebook Ireland Ltd', 'data': 'HTTP/1.1 301 Moved Permanently\r\nVary: Accept-Encoding\r\nLocation: https://www.facebook.com/\r\nContent-Type: text/html; charset="utf-8"\r\nX-FB-Debug: ryMLheDS9Y/10RtChKHVKi5BNNNfF4bl3zitEBlNHsseV3TKNfq8YBdEVE64P9DFrlWUOl+KvZm7bi77IoJS6A==\r\nDate: Sun, 15 Aug 2021 19:43:33 GMT\r\nAlt-Svc: h3-29=":443"; ma=3600,h3-27=":443"; ma=3600\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n', 'asn': 'AS32934', 'transport': 'tcp', 'ip_str': '31.13.82.17'}], 'asn': 'AS32934', 'isp': 'Facebook, Inc.', 'longitude': 139.69171, 'country_code3': None, 'domains': ['facebook.com'], 'ip_str': '31.13.82.17', 'os': None, 'ports': [80, 443]}
I'm a novice in Python and I'm having trouble with reading the API content and then looking in which dictionary and which list the information I want is in, etc. Is there an easier way than printing out elements down until you find the piece of data you want?
For example. I am trying to get to this element {'name': 'Famagusta District', 'lang': 'en'}
From this monstrosity:
{'address': 'Ammochostos', 'bbox': [33.548065185546875, 34.959442138671875, 34.590553283691406, 35.690277099609375], 'class_description': 'country, state, region,...', 'code': 'ADM1', 'confidence': 1, 'continent': 'EU', 'country': 'Cyprus', 'country_code': 'CY', 'country_geonames_id': '146669', 'description': 'first-order administrative division', 'dstOffset': 3, 'feature_class': 'A', 'geonames_id': 146615, 'lat': '35.33333', 'lng': '33.86667', 'ok': True, 'population': 46629, 'raw': {'timezone': {'gmtOffset': 2, 'timeZoneId': 'Asia/Famagusta', 'dstOffset': 3}, 'bbox': {'east': 34.590553283691406, 'south': 34.959442138671875, 'north': 35.690277099609375, 'west': 33.548065185546875, 'accuracyLevel': 0}, 'asciiName': 'Eparchia Ammochostou', 'astergdem': 133, 'countryId': '146669', 'fcl': 'A', 'srtm3': 134, 'countryCode': 'CY', 'adminCodes1': {'ISO3166_2': '04'}, 'adminId1': '146615', 'lat': '35.33333', 'fcode': 'ADM1', 'continentCode': 'EU', 'adminCode1': '01', 'lng': '33.86667', 'geonameId': 146615, 'toponymName': 'Eparchía Ammochóstou', 'adminTypeName': 'district', 'population': 46629, 'wikipediaURL': 'en.wikipedia.org/wiki/Famagusta_District', 'adminName5': '', 'adminName4': '', 'adminName3': '', 'alternateNames': [{'isPreferredName': True, 'name': '파마구스타 구', 'lang': 'ko'}, {'isShortName': True, 'isPreferredName': True, 'name': 'Ammochostos', 'lang': 'en'}, {'name': 'Ammóchostos', 'lang': 'el'}, {'name': 'Ammochostos District', 'lang': 'en'}, {'name': 'Eparchía Ammochóstou'}, {'name': 'Famagouste', 'lang': 'fr'}, {'isShortName': True, 'name': 'Famagusta', 'lang': 'en'}, {'name': 'Famagusta District', 'lang': 'en'}, {'name': 'Gazi Magosa İlçesi', 'lang': 'tr'}, {'name': 'Gazimağusa', 'lang': 'tr'}, {'name': 'https://en.wikipedia.org/wiki/Famagusta_District', 'lang': 'link'}, {'name': 'Magosa İlçesi', 'lang': 'tr'}, {'name': 'Q59148', 'lang': 'wkdt'}, {'name': 'Αμμόχωστος', 'lang': 'el'}, {'isPreferredName': True, 'name': 'Επαρχία Αμμοχώστου', 'lang': 'el'}], 'adminName2': '', 'name': 'Ammochostos', 'fclName': 'country, state, region,...', 'countryName': 'Cyprus', 'fcodeName': 'first-order administrative division', 'adminName1': 'Ammochostos'}, 'rawOffset': 2, 'srtm3': 134, 'state': 'Ammochostos', 'state_code': '01', 'state_geonames_id': '146615', 'status': 'OK', 'timeZoneId': 'Asia/Famagusta', 'timeZoneName': 'Asia/Famagusta', 'wikipedia': 'en.wikipedia.org/wiki/Famagusta_District'}
So Far I went so already 3 levels down but 4th layer seems to have at least dozen elements and AlternateNames is not one of them so it's at least 1 more level down.
print(row['features'][0]['properties'])
I am using Pycharm. Is there any way to view this with indentation or search function? If not what is the easiest way to read the elements down?
I'm not sure what name you have given to your dictionary. I have copied the text above and assigned it to a dictionary called 'dict1' for the purposes of showing you this answer.
dict1 = {'address': 'Ammochostos', 'bbox': [33.548065185546875, 34.959442138671875, 34.590553283691406, 35.690277099609375], 'class_description': 'country, state, region,...', 'code': 'ADM1', 'confidence': 1, 'continent': 'EU', 'country': 'Cyprus', 'country_code': 'CY', 'country_geonames_id': '146669', 'description': 'first-order administrative division', 'dstOffset': 3, 'feature_class': 'A', 'geonames_id': 146615, 'lat': '35.33333', 'lng': '33.86667', 'ok': True, 'population': 46629, 'raw': {'timezone': {'gmtOffset': 2, 'timeZoneId': 'Asia/Famagusta', 'dstOffset': 3}, 'bbox': {'east': 34.590553283691406, 'south': 34.959442138671875, 'north': 35.690277099609375, 'west': 33.548065185546875, 'accuracyLevel': 0}, 'asciiName': 'Eparchia Ammochostou', 'astergdem': 133, 'countryId': '146669', 'fcl': 'A', 'srtm3': 134, 'countryCode': 'CY', 'adminCodes1': {'ISO3166_2': '04'}, 'adminId1': '146615', 'lat': '35.33333', 'fcode': 'ADM1', 'continentCode': 'EU', 'adminCode1': '01', 'lng': '33.86667', 'geonameId': 146615, 'toponymName': 'Eparchía Ammochóstou', 'adminTypeName': 'district', 'population': 46629, 'wikipediaURL': 'en.wikipedia.org/wiki/Famagusta_District', 'adminName5': '', 'adminName4': '', 'adminName3': '', 'alternateNames': [{'isPreferredName': True, 'name': '파마구스타 구', 'lang': 'ko'}, {'isShortName': True, 'isPreferredName': True, 'name': 'Ammochostos', 'lang': 'en'}, {'name': 'Ammóchostos', 'lang': 'el'}, {'name': 'Ammochostos District', 'lang': 'en'}, {'name': 'Eparchía Ammochóstou'}, {'name': 'Famagouste', 'lang': 'fr'}, {'isShortName': True, 'name': 'Famagusta', 'lang': 'en'}, {'name': 'Famagusta District', 'lang': 'en'}, {'name': 'Gazi Magosa İlçesi', 'lang': 'tr'}, {'name': 'Gazimağusa', 'lang': 'tr'}, {'name': 'https://en.wikipedia.org/wiki/Famagusta_District', 'lang': 'link'}, {'name': 'Magosa İlçesi', 'lang': 'tr'}, {'name': 'Q59148', 'lang': 'wkdt'}, {'name': 'Αμμόχωστος', 'lang': 'el'}, {'isPreferredName': True, 'name': 'Επαρχία Αμμοχώστου', 'lang': 'el'}], 'adminName2': '', 'name': 'Ammochostos', 'fclName': 'country, state, region,...', 'countryName': 'Cyprus', 'fcodeName': 'first-order administrative division', 'adminName1': 'Ammochostos'}, 'rawOffset': 2, 'srtm3': 134, 'state': 'Ammochostos', 'state_code': '01', 'state_geonames_id': '146615', 'status': 'OK', 'timeZoneId': 'Asia/Famagusta', 'timeZoneName': 'Asia/Famagusta', 'wikipedia': 'en.wikipedia.org/wiki/Famagusta_District'}
Running dict1 alone will allow you to view how this looks in a much neater way. (I'm using Google Colab)
If I then run this line, it returns the data you are interested in:
dict1['raw']['alternateNames'][7]
This works because you must return the elements within the outer dictionary, and inner dictionaries. Once you reach 'alternateNames', you can find the information you want using list indexing.
An api return gives me this JSON:
'status': 200, 'result': [{'code': 'osgb4000000074565050', 'name_1': 'Durrington', 'name_1_lang': None, 'name_2': None, 'name_2_lang': None, 'local_type': 'Village', 'outcode': 'SP4', 'county_unitary': 'Wiltshire', 'county_unitary_type': 'UnitaryAuthority', 'district_borough': None, 'district_borough_type': None, 'region': 'South West', 'country': 'England', 'longitude': -1.77784130353605, 'latitude': 51.1989322399005, 'eastings': 415619, 'northings': 144436, 'min_eastings': 415126, 'min_northings': 143719, 'max_eastings': 416555, 'max_northings': 145363}, {'code': 'osgb4000000074573451', 'name_1': 'West Durrington', 'name_1_lang': None, 'name_2': None, 'name_2_lang': None, 'local_type': 'Suburban Area', 'outcode': 'BN13', 'county_unitary': 'West Sussex', 'county_unitary_type': 'County', 'district_borough': 'Worthing', 'district_borough_type': 'District', 'region': 'South East', 'country': 'England', 'longitude': -0.421810304962843, 'latitude': 50.8324879562536, 'eastings': 511236, 'northings': 104848, 'min_eastings': 510546, 'min_northings': 103835, 'max_eastings': 511721, 'max_northings': 105831}]}
I want to extract the "outcode". I have tried the following:
print(data.result.outcode)` and `print(data.result['outcode'])
and this:
for row in data:
print(result.outcode)
and:
for row in data:
print(['result'][0]['outcode'])
I have pretty much tried every variation that I can think of...
fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},
{'alias': 'sandwiches', 'title': 'Sandwiches'},
{'alias': 'salad', 'title': 'Salad'}],
'coordinates': {'latitude': 35.10871, 'longitude': -106.56739},
'display_phone': '(505) 881-5293',
'distance': 3571.724649307866,
'id': 'fork-and-fig-albuquerque',
'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/_-DpXKfS3jv6DyA47g6Fxg/o.jpg',
'is_closed': False,
'location': {'address1': '6904 Menaul Blvd NE',
'address2': 'Ste C',
'address3': '',
'city': 'Albuquerque',
'country': 'US',
'display_address': ['6904 Menaul Blvd NE', 'Ste C', 'Albuquerque, NM 87110'],
'state': 'NM',
'zip_code': '87110'},
'name': 'Fork & Fig',
'phone': '+15058815293',
'price': '$$',
'rating': 4.5,
'review_count': 604}
frontier_restaurant = {'categories': [{'alias': 'mexican', 'title': 'Mexican'},
{'alias': 'diners', 'title': 'Diners'},
{'alias': 'tradamerican', 'title': 'American (Traditional)'}],
'coordinates': {'latitude': 35.0808088832532, 'longitude': -106.619402244687},
'display_phone': '(505) 266-0550',
'distance': 4033.6583235266075,
'id': 'frontier-restaurant-albuquerque-2',
'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/M9L2z6-G0NobuDJ6YTh6VA/o.jpg',
'is_closed': True,
'location': {'address1': '2400 Central Ave SE',
'address2': '',
'address3': '',
'city': 'Albuquerque',
'country': 'US',
'display_address': ['2400 Central Ave SE', 'Albuquerque, NM 87106'],
'state': 'NM',
'zip_code': '87106'},
'name': 'Frontier Restaurant',
'phone': '+15052660550',
'price': '$',
'rating': 4.0,
'review_count': 1369}
I have two restaurant list as above and i want to make function that returns a list of only the restaurants that are not closed by using conditional loops.
restaurants = [fork_fig, frontier_restaurant]
open_restaurants(restaurants)[0]['name'] ###I want restaurant name to be appear
Below is the code that I've been working on and cannot quite sure how I can fix this to get the value that I want to return.
def open_restaurants(restaurants):
selected = []
for i in restaurants:
if fork_fig['is_closed']:
selected = restaurants[1]
else:
selected = restaurants[0]
return selected
Continuing from the comment:
fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},
{'alias': 'sandwiches', 'title': 'Sandwiches'},
{'alias': 'salad', 'title': 'Salad'}],
'coordinates': {'latitude': 35.10871, 'longitude': -106.56739},
'display_phone': '(505) 881-5293',
'distance': 3571.724649307866,
'id': 'fork-and-fig-albuquerque',
'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/_-DpXKfS3jv6DyA47g6Fxg/o.jpg',
'is_closed': False,
'location': {'address1': '6904 Menaul Blvd NE',
'address2': 'Ste C',
'address3': '',
'city': 'Albuquerque',
'country': 'US',
'display_address': ['6904 Menaul Blvd NE', 'Ste C', 'Albuquerque, NM 87110'],
'state': 'NM',
'zip_code': '87110'},
'name': 'Fork & Fig',
'phone': '+15058815293',
'price': '$$',
'rating': 4.5,
'review_count': 604}
frontier_restaurant = {'categories': [{'alias': 'mexican', 'title': 'Mexican'},
{'alias': 'diners', 'title': 'Diners'},
{'alias': 'tradamerican', 'title': 'American (Traditional)'}],
'coordinates': {'latitude': 35.0808088832532, 'longitude': -106.619402244687},
'display_phone': '(505) 266-0550',
'distance': 4033.6583235266075,
'id': 'frontier-restaurant-albuquerque-2',
'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/M9L2z6-G0NobuDJ6YTh6VA/o.jpg',
'is_closed': True,
'location': {'address1': '2400 Central Ave SE',
'address2': '',
'address3': '',
'city': 'Albuquerque',
'country': 'US',
'display_address': ['2400 Central Ave SE', 'Albuquerque, NM 87106'],
'state': 'NM',
'zip_code': '87106'},
'name': 'Frontier Restaurant',
'phone': '+15052660550',
'price': '$',
'rating': 4.0,
'review_count': 1369}
restaurants = [fork_fig, frontier_restaurant]
def open_restaurants(restaurants):
selected = []
for i in restaurants:
if 'is_closed' in i:
if not i['is_closed']:
selected.append(i['name'])
return selected
print(open_restaurants(restaurants))
OUTPUT:
['Fork & Fig']
Shorter-version:
Using list-comprehension:
def open_restaurants(restaurants):
return [x['name'] for x in restaurants if 'is_closed' in x and not x["is_closed"]]
print(open_restaurants(restaurants))
Using get() instead of indexing:
def open_restaurants(restaurants):
return [x['name'] for x in restaurants if 'name' in x and not x.get('is_closed', True)]
print(open_restaurants(restaurants))
Use the filter function:
def open_restaurants(restaurants):
return filter(lambda r: not r['is_closed'], restaurants)
Use List Comprehension here. Its one line and easy to read
open_restaurants = [restaurant.get("name") for restaurant in restaurants if not restaurant.get("is_closed")]
Output: ["Fork & Fig"]
There is a simple way to append all your open restaurants to a new list and print their names.
You will need two loops to accomplish this.
Here is how to do it:
restaurants = [fork_fig, frontier_restaurant] # a list of restaurants
def open_restaurants(restaurants):
selected = []
for i in restaurants: # append all open restaurants to a new list
if not i['is_closed']:
selected.append(i)
return selected
my_open_restaurants = open_restaurants(restaurants) # call the function
for rest in my_open_restaurants: # print all the names
print(rest['name'])
In your example the output will be: Fork & Fig