# Extracting Patient Information to store on external DB from stable study

**URL:** https://discourse.orthanc-server.org/t/extracting-patient-information-to-store-on-external-db-from-stable-study/2110
**Category:** Google Groups archive
**Created:** [September 21, 2020, 5:19am UTC](https://discourse.orthanc-server.org/t/extracting-patient-information-to-store-on-external-db-from-stable-study/2110 "2020-09-21T05:19:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kavi\_Joshi](https://discourse.orthanc-server.org/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@Kavi\_Joshi](https://discourse.orthanc-server.org/u/Kavi_Joshi)
#### Post date: [September 21, 2020, 5:19am UTC](https://discourse.orthanc-server.org/t/extracting-patient-information-to-store-on-external-db-from-stable-study/2110/1 "2020-09-21T05:19:00Z")

</div>

Hi  
I am trying to extract following patient information for the received studies and forward it to another PACS.

1. Patient Name
2. Institution
3. Modality
4. Study Date
5. Study Time  
I am currently using OnstoredInstance Lua script which works fine to extract the tags and post to external DB. But it is creating HTTP API POST calls for every instance which is not desirable for me as it overloads the DB receiving many other query from other sources as well. I want to extract this information only when the study is stable (to reduce the number of queries to my DB), for this i tried to use the following nested Lua script but it only sends the Institution, Study Date and Time. It doesnot send the Patient Name and Modality.  
Any suggestions??

**Lua Script is below:**

JSON = (load(HttpGet(‘[http://regex.info/code/JSON.lua](http://regex.info/code/JSON.lua)’))) ()

function OnStableStudy(studyId, tags, metadata)

local headers = { [“content-type”] = “application/json; charset=utf-8” }

function OnStoredInstance(instanceId, tags, metadata)  
local info = {}  
info[‘InstitutionName’] = tags[‘InstitutionName’]  
info[‘PatientName’] = tags[‘PatientName’]  
info[‘Modality’] = tags[‘Modality’]  
info[‘StudyInstanceUID’] = tags[‘StudyInstanceUID’]  
info[‘StudyDate’] = tags[‘StudyDate’]  
info[‘StudyTime’] = tags[‘StudyTime’]  
return info  
end

local test = OnStoredInstance(instanceId, tags, metadata)

– Send the POST request  
local answer = HttpPost(‘[http://localhost:3000/api/dataFromPacsRouter](http://localhost:3000/api/dataFromPacsRouter)’, JSON:encode(test), headers)

– The answer equals “ERROR” in case of an error  
print('Web service called, answer received: ’ … answer)

local moveBody = {}  
moveBody[“Synchronous”] = false  
moveBody[“Resources”] = {}  
table.insert(moveBody[“Resources”], studyId)

local moveJobId = ParseJson(RestApiPost(‘/modalities/ORTHANCLOCAL/store’, DumpJson(moveBody)))[“ID”]  
print('moving current study to workstation in job ’ … moveJobId)

end

function OnJobSuccess(jobId)  
local job = ParseJson(RestApiGet(‘/jobs/’ … jobId))

if job[“Type”] == “DicomModalityStore” then  
– delete the study once it has been forwarded  
RestApiDelete(‘/studies/’ … job[“Content”][“ParentResources”][1])  
end  
end

---

<div class="post-metadata">

### Author: ![jodogne](https://discourse.orthanc-server.org/user_avatar/discourse.orthanc-server.org/jodogne/32/1511_2.png) [@jodogne](https://discourse.orthanc-server.org/u/jodogne)
#### Post date: [September 21, 2020, 6:46am UTC](https://discourse.orthanc-server.org/t/extracting-patient-information-to-store-on-external-db-from-stable-study/2110/2 "2020-09-21T06:46:40Z")

</div>

Hello,

The “PatientName” tag belongs to the patient level, whereas the “Modality” tag belongs to the series level (in DICOM, different series in the same study can be of different modalities, think of PET/CT for instance).

Here is a full working sample showing how to access the required information:

function OnStableStudy(studyId, tags, metadata)  
study = ParseJson(RestApiGet(‘/studies/’ … studyId))

institution = study[‘MainDicomTags’][‘InstitutionName’]  
studyDate = study[‘MainDicomTags’][‘StudyDate’]  
studyTime = study[‘MainDicomTags’][‘StudyTime’]  
patientName = study[‘PatientMainDicomTags’][‘PatientName’]

print(institution, studyDate, studyTime, patientName)

for index, seriesId in ipairs(study[‘Series’]) do  
series = ParseJson(RestApiGet(‘/series/’ … seriesId))  
print(’ Modality ’ … index … ': ’ … series[‘MainDicomTags’][‘Modality’])  
end  
end

Sébastien-

---

<div class="post-metadata">

### Author: ![Kavi\_Joshi](https://discourse.orthanc-server.org/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@Kavi\_Joshi](https://discourse.orthanc-server.org/u/Kavi_Joshi)
#### Post date: [September 21, 2020, 10:36am UTC](https://discourse.orthanc-server.org/t/extracting-patient-information-to-store-on-external-db-from-stable-study/2110/3 "2020-09-21T10:36:01Z")

</div>

Thank you Sébastien

This resolved my problem.

Regards  
Kavi
