简体   繁体   中英

Fetching the value from the key in Python's dict

I am fetching 'value' from 'key' in JSON but I do not know why I cannot fetch the target information. Code below

import json 
import requests
 
#Person's ID
id=1194452

#Url
info=requests.get(f'https://api.brokercheck.finra.org/search/individual/{id}?hl=true&includePrevious=true&sort=bc_lastname_sort+asc,bc_firstname_sort+asc,bc_middlename_sort+asc,score+desc&wt=json') 

#convert to JSON
x=info.json()

 #Value
 print(x["firstName"])

The following is JSON of this example. (Information in the link is available to public. I, thus, do not delete the details of 1194452).

{'hits': {'total': 1, 'hits': [{'_type': '_doc', '_source': {'content': '{"basicInformation": {"individualId": 1194452, "firstName": "STEPHEN", "middleName": "MICHAEL", "lastName": "SCHULTZ", "otherNames": [], "bcScope": "Active", "iaScope": "NotInScope", "daysInIndustryCalculatedDate": "4/11/1984"}, "currentEmployments": [{"firmId": 5685, "firmName": "PRUCO SECURITIES, LLC.", "iaOnly": "N", "registrationBeginDate": "4/12/1984", "firmBCScope": "ACTIVE", "firmIAScope": "ACTIVE", "iaSECNumber": "52208", "iaSECNumberType": "801", "bdSECNumber": "16402", "branchOfficeLocations": [{"locatedAtFlag": "Y", "supervisedFromFlag": "N", "privateResidenceFlag": "N", "branchOfficeId": "737412", "street1": "445 Broadhollow Road", "street2": "Suite 405", "city": "Melville", "cityAlias": ["DIX HILLS", "HUNTINGTN STA", "HUNTINGTON STATION", "MELVILLE"], "state": "NY", "country": "United States", "zipCode": "11747", "latitude": "40.785118", "longitude": "-73.404965", "geoLocation": "40.785118,-73.404965", "nonRegisteredOfficeFlag": "N", "elaBeginDate": "10/15/2021"}]}], "currentIAEmployments": [], "previousEmployments": [], "previousIAEmployments": [], "disclosureFlag": "N", "iaDisclosureFlag": "N", "disclosures": [], "examsCount": {"stateExamCount": 0, "principalExamCount": 0, "productExamCount": 2}, "stateExamCategory": [], "principalExamCategory": [], "productExamCategory": [{"examCategory": "SIE", "examName": "Securities Industry Essentials Examination", "examTakenDate": "10/1/2018", "examScope": "BC"}, {"examCategory": "Series 6", "examName": "Investment Company Products/Variable Contracts Representative Examination", "examTakenDate": "4/2/1984", "examScope": "BC"}], "registrationCount": {"approvedSRORegistrationCount": 1, "approvedFinraRegistrationCount": 1, "approvedStateRegistrationCount": 1, "approvedIAStateRegistrationCount": 0}, "registeredStates": [{"state": "New York", "regScope": "BC", "status": "APPROVED", "regDate": "4/12/1984"}], "registeredSROs": [{"sro": "FINRA", "status": "APPROVED"}], "brokerDetails": {"hasBCComments": "N", "hasIAComments": "N", "legacyReportStatusDescription": "Not Requested"}}'}}]}}

Questions

  1. Since type(x) is 'dict' in Python, why can't I fetch the value of the key?
  2. What is 'hits' in JSON? I googled 'hits' and find that it is 'Hyperlink Induced Topic Search'.

Thank you

In this case, hits is the numbers of results of your query. It's just the keys this API is using to structure its request response.

In order to get firstName , you have to navigate along the nested dictionaries (accessing elements with a key) and lists (accessing elements with an index). Since you only have one hit in your case (giving a person's ID and receiving a response with total=1), you'll access the unique hit with index 0.

One thing to look out for though: the value associated with content is a json string. So you should read it as json.

In order to print all first names:

import json

x = {'hits': {'total': 1, 'hits': [{'_type': '_doc', '_source': {'content': '{"basicInformation": {"individualId": 1194452, "firstName": "STEPHEN", "middleName": "MICHAEL", "lastName": "SCHULTZ", "otherNames": [], "bcScope": "Active", "iaScope": "NotInScope", "daysInIndustryCalculatedDate": "4/11/1984"}, "currentEmployments": [{"firmId": 5685, "firmName": "PRUCO SECURITIES, LLC.", "iaOnly": "N", "registrationBeginDate": "4/12/1984", "firmBCScope": "ACTIVE", "firmIAScope": "ACTIVE", "iaSECNumber": "52208", "iaSECNumberType": "801", "bdSECNumber": "16402", "branchOfficeLocations": [{"locatedAtFlag": "Y", "supervisedFromFlag": "N", "privateResidenceFlag": "N", "branchOfficeId": "737412", "street1": "445 Broadhollow Road", "street2": "Suite 405", "city": "Melville", "cityAlias": ["DIX HILLS", "HUNTINGTN STA", "HUNTINGTON STATION", "MELVILLE"], "state": "NY", "country": "United States", "zipCode": "11747", "latitude": "40.785118", "longitude": "-73.404965", "geoLocation": "40.785118,-73.404965", "nonRegisteredOfficeFlag": "N", "elaBeginDate": "10/15/2021"}]}], "currentIAEmployments": [], "previousEmployments": [], "previousIAEmployments": [], "disclosureFlag": "N", "iaDisclosureFlag": "N", "disclosures": [], "examsCount": {"stateExamCount": 0, "principalExamCount": 0, "productExamCount": 2}, "stateExamCategory": [], "principalExamCategory": [], "productExamCategory": [{"examCategory": "SIE", "examName": "Securities Industry Essentials Examination", "examTakenDate": "10/1/2018", "examScope": "BC"}, {"examCategory": "Series 6", "examName": "Investment Company Products/Variable Contracts Representative Examination", "examTakenDate": "4/2/1984", "examScope": "BC"}], "registrationCount": {"approvedSRORegistrationCount": 1, "approvedFinraRegistrationCount": 1, "approvedStateRegistrationCount": 1, "approvedIAStateRegistrationCount": 0}, "registeredStates": [{"state": "New York", "regScope": "BC", "status": "APPROVED", "regDate": "4/12/1984"}], "registeredSROs": [{"sro": "FINRA", "status": "APPROVED"}], "brokerDetails": {"hasBCComments": "N", "hasIAComments": "N", "legacyReportStatusDescription": "Not Requested"}}'}}]}}

for hit in x['hits']['hits']:
    person_json = json.loads(hit['_source']['content'])
    print(person_json['basicInformation']['firstName'])

Or get the first name of the first (and in this case only) hit:

json.loads(x['hits']['hits'][0]['_source']['content'])['basicInformation']['firstName']

API response as a JSON object but the required data is in string form of JSON object Data. So You have to call json.loads() to make into dict and hits means listing object of data.

Script:

import requests
import json

id = 1194452
url = f'https://api.brokercheck.finra.org/search/individual/{id}?hl=true&includePrevious=true&sort=bc_lastname_sort+asc,bc_firstname_sort+asc,bc_middlename_sort+asc,score+desc&wt=json'

response = requests.get(url)
data = response.json()

for hit in data['hits']['hits']:
    content_str = hit['_source']['content']
    content = json.loads(content_str)
    first_name = content['basicInformation']['firstName']
    print(first_name)

Output:

STEPHEN

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-2025 STACKOOM.COM