Я пытаюсь играть с открытым API FDA. Пока все работает хорошо. Проблема для вложенных данных JSON.
Вот мои данные Json:
{
"seriousnessother": "1",
"reportduplicate": {
"duplicatenumb": "US-BRISTOL-MYERS SQUIBB COMPANY-BMS-2017-086135",
"duplicatesource": "BRISTOL MYERS SQUIBB"
},
"safetyreportversion": "1",
"receiptdate": "20170927",
"duplicate": "1",
"seriousnessdeath": "1",
"receivedate": "20170927",
"patient": {
"reaction": [
{
"reactionmeddrapt": "Death",
"reactionmeddraversionpt": "20.1",
"reactionoutcome": "5"
},
{
"reactionmeddrapt": "Product use in unapproved indication",
"reactionmeddraversionpt": "20.1",
"reactionoutcome": "6"
}
],
"patientsex": "1",
"drug": [
{
"drugstartdateformat": "102",
"medicinalproduct": "OPDIVO",
"drugindication": "COLORECTAL CANCER",
"drugcharacterization": "1",
"drugadministrationroute": "065",
"drugenddateformat": "102",
"drugseparatedosagenumb": "1",
"drugstructuredosageunit": "032",
"openfda": {
"manufacturer_name": [
"E.R. Squibb & Sons, L.L.C."
],
"unii": [
"31YO63LBSN"
],
"product_type": [
"HUMAN PRESCRIPTION DRUG"
],
"spl_set_id": [
"f570b9c4-6846-4de2-abfa-4d0a4ae4e394"
],
"route": [
"INTRAVENOUS"
],
"generic_name": [
"NIVOLUMAB"
],
"brand_name": [
"OPDIVO"
],
"product_ndc": [
"0003-3772",
"0003-3734",
"0003-3774"
],
"pharm_class_epc": [
"Programmed Death Receptor-1 Blocking Antibody [EPC]"
],
"substance_name": [
"NIVOLUMAB"
],
"spl_id": [
"2d33126d-5115-459e-bcaf-d0ace4fbe94e"
],
"pharm_class_moa": [
"Programmed Death Receptor-1-directed Antibody Interactions [MoA]"
],
"application_number": [
"BLA125554"
],
"nui": [
"N0000191259",
"N0000191260"
],
"package_ndc": [
"0003-3734-13",
"0003-3772-11",
"0003-3774-12"
]
},
"drugstructuredosagenumb": "1",
"drugintervaldosageunitnumb": "2",
"drugstartdate": "20160907",
"actiondrug": "5",
"activesubstance": {
"activesubstancename": "NIVOLUMAB"
},
"drugintervaldosagedefinition": "803",
"drugauthorizationnumb": "125554",
"drugrecurreadministration": "3",
"drugdosagetext": "1 DF, Q2WK",
"drugenddate": "20161222",
"drugadditional": "3"
}
]
},
"occurcountry": "US",
"reporttype": "1",
"companynumb": "US-BRISTOL-MYERS SQUIBB COMPANY-BMS-2017-086135",
"safetyreportid": "14015990",
"sender": {
"senderorganization": "FDA-Public Use",
"sendertype": "2"
},
"transmissiondate": "20171128",
"fulfillexpeditecriteria": "1",
"transmissiondateformat": "102",
"receiptdateformat": "102",
"receiver": {
"receiverorganization": "FDA",
"receivertype": "6"
},
"serious": "1",
"receivedateformat": "102",
"primarysource": {
"reportercountry": "US",
"qualification": "5"
},
"primarysourcecountry": "US"
}
Вот мой взгляд на вызов этих данных и преобразование их в данные Django.
json_data = open('/users/pulkitsharma/downloads/drug-bad.json').read()
response = json.loads(json_data)
a=0
b=0
for data in response['results']:
#try:
seriousnessother = data.get('seriousnessother')
reportduplicate_duplicatenumb = data['reportduplicate'].get('duplicatenumb')
reportduplicate_duplicatesource = data['reportduplicate'].get('duplicatesource')
safetyreportversion = data.get('safetyreportversion')
receiptdate = data.get('receiptdate')
duplicate = data.get('duplicate')
seriousnessdeath = data.get('seriousnessdeath')
receivedate = data.get('receivedate')
patient_reaction_reactionmeddrapt = data['patient']['reaction'].get('reactionmeddrapt')
patient_reaction_reactionmeddraversionpt = data['patient']['reaction'].get('reactionmeddraversionpt')
patient_reaction_reactionoutcome = data['patient']['reaction'].get('reactionoutcome')
patient_patientsex = data['patient'].get('patientsex')
patient_drug_medicinalproduct = data['patient']['drug'].get('medicinalproduct')
patient_drug_drugindication = data['patient']['drug'].get('drugindication')
patient_drug_drugcharacterization = data['patient']['drug'].get('drugcharacterization')
patient_drug_drugadministrationroute = data['patient']['drug'].get('drugadministrationroute')
patient_drug_drugseparatedosagenumb = data['patient']['drug'].get('drugseparatedosagenumb')
patient_drug_drugstructuredosageunit = data['patient']['drug'].get('drugstructuredosageunit')
patient_drug_openfda_manufacturer_name = data['patient']['drug']['openfda']['manufacturer'].get('name')
patient_drug_openfda_unii = data['patient']['drug']['openfda'].get('unii')
patient_drug_openfda_product_type = data['patient']['drug']['openfda']['product'].get('type')
patient_drug_openfda_spl_set_id = data['patient']['drug']['openfda']['spl']['set'].get('id')
patient_drug_openfda_route = data['patient']['drug']['openfda'].get('route')
patient_drug_openfda_generic_name = data['patient']['drug']['openfda']['generic'].get('name')
patient_drug_openfda_brand_name = data['patient']['drug']['openfda']['brand'].get('name')
patient_drug_openfda_product_ndc = data['patient']['drug']['openfda']['product'].get('ndc')
patient_drug_openfda_pharm_class_epc = data['patient']['drug']['openfda']['pharm']['class'].get('epc')
patient_drug_openfda_substance_name = data['patient']['drug']['openfda']['substance'].get('name')
patient_drug_openfda_spl_id = data['patient']['drug']['openfda']['spl'].get('id')
patient_drug_openfda_pharm_class_moa = data['patient']['drug']['openfda']['pharm']['class'].get('moa')
patient_drug_openfda_application_number = data['patient']['drug']['openfda']['application'].get('number')
patient_drug_openfda_nui = data['patient']['drug']['openfda'].get('nui')
patient_drug_openfda_package_ndc = data['patient']['drug']['openfda']['package'].get('ndc')
patient_drug_drugstructuredosagenumb = data['patient']['drug'].get('drugstructuredosagenumb')
patient_drug_drugintervaldosageunitnumb = data['patient']['drug'].get('drugintervaldosageunitnumb')
patient_drug_drugstartdate = data['patient']['drug'].get('drugstartdate')
patient_drug_actiondrug = data['patient']['drug'].get('actiondrug')
patient_drug_activesubstance_activesubstancename = data['patient']['drug']['activesubstance'].get('activesubstancename')
patient_drug_drugintervaldosagedefinition = data['patient']['drug'].get('drugintervaldosagedefinition')
patient_drug_drugauthorizationnumb = data['patient']['drug'].get('drugauthorizationnumb')
patient_drug_drugrecurreadministration = data['patient']['drug'].get('drugrecurreadministration')
patient_drug_drugdosagetext = data['patient']['drug'].get('drugdosagetext')
patient_drug_drugenddate = data['patient']['drug'].get('drugenddate')
patient_drug_drugadditional = data['patient']['drug'].get('drugadditional')
occurcountry = data.get('occurcountry')
reporttype = data.get('reporttype')
companynumb = data.get('companynumb')
safetyreportid = data.get('safetyreportid')
sender_senderorganization = data['sender'].get('senderorganization')
sender_sendertype = data['sender'].get('sendertype')
fulfillexpeditecriteria = data.get('fulfillexpeditecriteria')
receiver_receiverorganization = data['receiver'].get('receiverorganization')
receiver_receivertype = data['receiver'].get('receivertype')
serious = data.get('serious')
primarysource_reportercountry = data['primarysource'].get('reportercountry')
primarysource_qualification = data['primarysource'].get('qualification')
primarysourcecountry = data.get('primarysourcecountry')
Я получаю следующую ошибку:
patient_reaction_reactionmeddrapt = data['patient']['reaction'].get('reactionmeddrapt')
AttributeError: 'list' object has no attribute 'get'
Обратите внимание, что в результате ответа много данных. Может быть так, что у одного из результатов нет поля пациента. Но метод get должен возвращать None, если он не существует.
Я хочу либо значение поля, либо None.
Это работает для простых данных Json, но ошибка возникает, когда внутри массива / списка есть массив / список.