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
- Since type(x) is ‘dict’ in Python, why can’t I fetch the value of the key?
- What is ‘hits’ in JSON? I googled ‘hits’ and find that it is ‘Hyperlink Induced Topic Search’.
Thank you
2
Answers
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:
Or get the first name of the first (and in this case only) hit:
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 andhits
means listing object of data.Script:
Output: