0

I'm not familiar with JSON and FHIR. I have a request to a FHIR server that returns medication dispense object. Here is the result of the request contain in a string

{"resourceType":"Bundle","type":"document","entry":[{"fullUrl":"/medicationdispense/c7c7e373-02c0-4ffc-9894-eee0de249a25/1000424","resource":{"resourceType":"MedicationDispense","id":"1","extension":[{"url":"uri:domedic:pharmacy:uuid","valueString":"dc28f64f-aef4-4d64-8cb5-b5a2020fdcdc"},{"url":"uri:domedic:prescription:renewals:left","valueInteger":24},{"url":"uri:domedic:medication:isoob","valueBoolean":true}],"status":"on-hold","category":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/medicationrequest-category","code":"community"}],"text":"Includes requests for medications to be administered or consumed by the patient in their home (this would include long term care or nursing homes, hospices, etc.)"},"medicationCodeableConcept":{"extension":[{"url":"uri:domedic:medication:name","valueString":"SANDOZ FENTANYL"},{"url":"uri:domedic:medication:strength","valueString":"50 MCG-H"},{"url":"uri:domedic:medication:form","valueString":"TIMBRE"},{"url":"http://hl7.org/fhir/NamingSystem/ca-hc-din","valueString":"02327147"}],"coding":[{"system":"http://hl7.org/fhir/NamingSystem/ca-hc-din","code":"02327147","display":"SANDOZ FENTANYL 50 MCG-H"}],"text":"SANDOZ FENTANYL 50 MCG-H"},"subject":{"reference":"patient/c7c7e373-02c0-4ffc-9894-eee0de249a25","identifier":{"use":"official","system":"uri:domedic:patient:uuid","value":"c7c7e373-02c0-4ffc-9894-eee0de249a25"}},"authorizingPrescription":[{"reference":"medicationrequest/c7c7e373-02c0-4ffc-9894-eee0de249a25/1000424","identifier":{"use":"usual","system":"uri:domedic:medicationrequest:number","value":"1000424"}}],"quantity":{"value":2.0000},"whenPrepared":"2021-10-29T00:00:00+00:00","dosageInstruction":[{"extension":[{"url":"uri:domedic:dosage:discriminator","valueInteger":2},{"url":"uri:domedic:dosage:id","valueInteger":12042}],"sequence":1,"text":"1ERE RX EN DATE DU JOUR PUIS LES 2 AUTRES SERONT DES REN. POSTDATES. CESSATION FUTURE SUIVRA","timing":{"event":["2021-10-29T08:00:00+00:00","2021-10-29T21:00:00+00:00"],"repeat":{"boundsPeriod":{"start":"2021-10-29T00:00:00+00:00"},"period":3.0,"periodUnit":"d"}},"doseAndRate":[{"doseQuantity":{"value":1.0000}}]},{"extension":[{"url":"uri:domedic:dosage:discriminator","valueInteger":2},{"url":"uri:domedic:dosage:id","valueInteger":12042}],"sequence":2,"text":"1ERE RX EN DATE DU JOUR PUIS LES 2 AUTRES SERONT DES REN. POSTDATES. CESSATION FUTURE SUIVRA","timing":{"event":["2021-10-30T08:00:00+00:00","2021-10-30T21:00:00+00:00","2021-10-31T08:00:00+00:00","2021-10-31T21:00:00+00:00"],"repeat":{"boundsPeriod":{"start":"2021-10-29T00:00:00+00:00"},"period":3.0,"periodUnit":"d"}},"doseAndRate":[{"doseQuantity":{"value":0.0000}}]}]}}]}

I need to "cast" this to a HL7.FHIR.Model.MedicationDispense type object, but I can't find how to do it.

can anyone help me on this please? thank for your time and help

Dominic
  • 159
  • 1
  • 3
  • 13
  • This might help: https://stackoverflow.com/a/48023576/4180382 – Ole EH Dufour Nov 16 '22 at 20:23
  • 3
    What you're getting is of type Bundle, not type MedicationRequest. The MedicationRequest bit is nested within. The instance you're receiving is also not conformant - FHIR documents are required to start with a Composition entry, which this one isn't. I have no idea why the server is wrapping your MedicationDispense inside a Bundle, as it's not accomplishing anything by doing so beyond breaking conformance. – Lloyd McKenzie Nov 16 '22 at 20:28

1 Answers1

0

I have written an article of how to build up util methods to extract data from FHIR bundles here: https://toreaurstad.blogspot.com/2022/06/making-use-of-extension-methods-to.html

You say you use HL7.Fhir, so check that you have got these packages (version may vary to your needs)

  <PackageReference Include="Hl7.Fhir.R4" Version="4.0.0" />
  <PackageReference Include="Hl7.Fhir.Serialization" Version="4.0.0" />
  <PackageReference Include="Hl7.Fhir.Support" Version="4.0.0" />
  <PackageReference Include="Hl7.Fhir.Support.Poco" Version="4.0.0" />

To extract medication statement dosages, a concept in FHIR, you could :

Create a wrapper property with logic in a class that will be your domain model. This is just a class that contains your data, it will be built up from datda inside the Json bundle.

  public int? Fentanyl
   {
            get
            {
                var dosageQuantity = _bundle.SearchMedicationStatements("http://someacme.no/fhir/MedicationStatement/")
                    ?.GetMedicationDosageQuantity("Fentanyl", "ug");
                //value is already a decimal? data type and must be parsed 
                if (int.TryParse(dosageQuantity?.Value?.ToString(), out var dosageQuantityParsed))
                {
                    return dosageQuantityParsed;
                }
                return null;
            }
    }

 
    public static List<MedicationStatement>? SearchMedicationStatements(this Bundle bundle, string resourcePath)
    {
        var medicationStatementsMatching = bundle?.Entry?.Where(e => e.FullUrl.StartsWith(resourcePath))?.Select(m => m.Resource)?.OfType<MedicationStatement>()?.ToList();
        return medicationStatementsMatching;
    }

    public static Dosage? GetMedicationDosageDosage(this List<MedicationStatement> medicationStatements, string displayText)
    {
        //find dosage with given display text 

        foreach (var medicationStatement in medicationStatements)
        {
            var medicationStatementMedication = medicationStatement?.Medication as CodeableConcept; 
            if (medicationStatementMedication == null)
            {
                continue; 
            }
            var medicationCoding = medicationStatementMedication?.Coding?.FirstOrDefault(med => med.Display?.Equals(displayText, StringComparison.InvariantCultureIgnoreCase) == true);  
            if (medicationCoding != null)
            {
                var quantity = medicationStatement?.Dosage?.FirstOrDefault();
                return quantity; 
            }           
        }

        return null;
    }

Of course, there are more to this than just your domain model. You must pass in the bundle of type Bundle into this domain model, for example via constructor of the domain model.

Something like this:

var bundle = new FhirJsonParser().Parse(await result.Content.ReadAsStringAsync());

I will suggest you for example approach your FHIR mapping using:

  • A domain model as suggested here
  • Pass in the bundle
  • Keep logic inside the getters of properties that constitute the domain model, which contains the interpreted data using the HL7.Fhir libs
  • You probably want to avoid repeating code and have additional helper methods, such as the medication dosage helper methods I showed you here. Use the safe navigator "?" here to chain calls to extract the FHIR data. Sometimes, some FHIR data might not be filled in, so using "?" to do null checks are practical here.
Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22