ORCA / web API / python / 情報の取り出し

# libraryのimport
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# # BASIC認証 webAPIにrequest
res = requests.get('http://ip-address:8000/api01rv2/patientgetv2?id=36817&format=json', auth=HTTPBasicAuth('id', 'pw'))
res.json()
{'patientinfores': {'Information_Date': '2021-10-11',
  'Information_Time': '16:58:35',
  'Api_Result': '00',
  'Api_Result_Message': '処理終了',
  'Reskey': 'Patient Info',
  'Patient_Information': {'Patient_ID': '0036817',
   'WholeName': '日医\u3000太郎',
   'WholeName_inKana': 'ニチイ\u3000タロウ',
   'BirthDate': '1948-03-27',
   'Sex': '1',
   'HouseHolder_WholeName': 'ニチイ',
   'Home_Address_Information': {'Address_ZipCode': '9493333',
    'WholeAddress1': '上越市〇〇区××',
    'WholeAddress2': '283−2',
    'PhoneNumber1': '025-555-9999',
    'PhoneNumber2': '090-4444-1111'},
   'Comment1': 'H27.01.13来院',
   'TestPatient_Flag': '0',
   'Reduction_Reason': '00',
   'Reduction_Reason_Name': '該当なし',
   'Discount': '00',
   'Discount_Name': '該当なし',
   'Condition1': '00',
   'Condition1_Name': '該当なし',
   'Condition2': '00',
   'Condition2_Name': '該当なし',
   'Condition3': '00',
   'Condition3_Name': '該当なし',
   'Ic_Code': '01',
   'Ic_Code_Name': '現金',
   'Community_Cid_Agree': 'False',
   'FirstVisit_Date': '2015-03-10',
   'LastVisit_Date': '2021-10-05',
   'HealthInsurance_Information': [{'Insurance_Combination_Number': '0003',
     'InsuranceCombination_Rate_Admission': '0.20',
     'InsuranceCombination_Rate_Outpatient': '0.20',
     'Insurance_Nondisplay': 'N',
     'InsuranceProvider_Class': '060',
     'InsuranceProvider_Number': '150037',
     'InsuranceProvider_WholeName': '国保',
     'HealthInsuredPerson_Symbol': '〇〇',
     'HealthInsuredPerson_Number': '35000000',
     'HealthInsuredPerson_Branch_Number': '06',
     'HealthInsuredPerson_Assistance': '2',
     'HealthInsuredPerson_Assistance_Name': '2割',
     'RelationToInsuredPerson': '2',
     'HealthInsuredPerson_WholeName': '日医\u3000太郎',
     'Certificate_StartDate': '2021-08-10',
     'Certificate_ExpiredDate': '2022-07-31',
     'Certificate_GetDate': '2021-08-01',
     'Insurance_CheckDate': '2021-08-10'},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}]}}}
result = res.json()
# IDを取り出したい
result['patientinfores']['Patient_Information']['Patient_ID']
'0036817'
patient_info = result['patientinfores']['Patient_Information']
pd.json_normalize(patient_info)
Patient_ID WholeName WholeName_inKana BirthDate Sex HouseHolder_WholeName Comment1 TestPatient_Flag Reduction_Reason Reduction_Reason_Name ... Ic_Code_Name Community_Cid_Agree FirstVisit_Date LastVisit_Date HealthInsurance_Information Home_Address_Information.Address_ZipCode Home_Address_Information.WholeAddress1 Home_Address_Information.WholeAddress2 Home_Address_Information.PhoneNumber1 Home_Address_Information.PhoneNumber2
0 0036817 日医 太郎 ニチイ タロウ 1945-01-01 1 日医 H27.01.13来院 0 00 該当なし ... 現金 False 2015-03-10 2021-10-05 [{'Insurance_Combination_Number': '0003', 'Ins... 9493333 上越市〇〇区×× 283−2 025-555-9999 090-4444-1111

1 rows × 29 columns

df = pd.json_normalize(patient_info)
df.columns
Index(['Patient_ID', 'WholeName', 'WholeName_inKana', 'BirthDate', 'Sex',
       'HouseHolder_WholeName', 'Comment1', 'TestPatient_Flag',
       'Reduction_Reason', 'Reduction_Reason_Name', 'Discount',
       'Discount_Name', 'Condition1', 'Condition1_Name', 'Condition2',
       'Condition2_Name', 'Condition3', 'Condition3_Name', 'Ic_Code',
       'Ic_Code_Name', 'Community_Cid_Agree', 'FirstVisit_Date',
       'LastVisit_Date', 'HealthInsurance_Information',
       'Home_Address_Information.Address_ZipCode',
       'Home_Address_Information.WholeAddress1',
       'Home_Address_Information.WholeAddress2',
       'Home_Address_Information.PhoneNumber1',
       'Home_Address_Information.PhoneNumber2'],
      dtype='object')
# 必要なcolumnを指定する
df[['Patient_ID', 'WholeName', 'WholeName_inKana', 'BirthDate', 'Sex','Home_Address_Information.Address_ZipCode',
       'Home_Address_Information.WholeAddress1',
       'Home_Address_Information.WholeAddress2']]
Patient_ID WholeName WholeName_inKana BirthDate Sex Home_Address_Information.Address_ZipCode Home_Address_Information.WholeAddress1 Home_Address_Information.WholeAddress2
0 0036817 日医 太郎 ニチイ タロウ 1945-01-01 1 9493333 上越市〇〇区××/td> 283−2
# .csvで書き出す
df[['Patient_ID', 'WholeName', 'WholeName_inKana', 'BirthDate', 'Sex','Home_Address_Information.Address_ZipCode',
       'Home_Address_Information.WholeAddress1',
       'Home_Address_Information.WholeAddress2']].to_csv('patient-info.csv',index=False)
# patient-info.csvというファイルが書き出された