将Fhir JSON响应解析为一个简单的对象。

huangapple go评论96阅读模式
英文:

Parsing Fhir json response into a simple object

问题

Here is the translated code snippet you provided:

var response = await _fhirClient.OperationAsync(new Uri(EndPointUrl + "FHIR/STU3/ReferralRequest"), "ers.fetchworklist", paramResource);

var eReferrals = new List<eReferral>();

if (response != null)
{
    var patientNode = FhirJsonNode.Parse(await response.ToJsonAsync());
    var entries = patientNode.Children("entry").ToList();

    foreach (var entry in entries)
    {
        var extensions = entry.Children("extension").ToList();

        // Extracting relevant data
        var priorityUrl = extensions[0]["url"]?.ToString();
        var priorityValue = extensions[0]["valueCodeableConcept"]["coding"][0]["code"]?.ToString();
        var specialtyUrl = extensions[1]["url"]?.ToString();
        var specialtyValue = extensions[1]["valueCodeableConcept"]["coding"][0]["code"]?.ToString();

        // You can continue extracting other fields in a similar manner

        eReferrals.Add(new eReferral
        {
            Priority = priorityValue,
            Specialty = specialtyValue,
            // Add other properties here
        });
    }
}

return eReferrals;

This code parses the JSON response and extracts the "priority" and "specialty" values for each entry into a list of eReferral objects. You can follow a similar approach to extract other elements you need.

英文:

Hi I've been trying to fathom this all day and frankly it's beyond me.

So I've got some json response from a firely c# call and I want to get certain elements into a nice list of objects that are easier to handle and display the data back to the user.

My object

public class eReferral
    {

        public string Reference { get; set; }

        public string Priority { get; set; }

        public string Specialty { get; set; }

        public string Patient { get; set; }

    }

The json

{
&quot;meta&quot;: {
&quot;profile&quot;: [
&quot;https://fhir.nhs.uk/STU3/StructureDefinition/eRS-FetchWorklist-List-1&quot;
]
},
&quot;resourceType&quot;: &quot;List&quot;,
&quot;contained&quot;: [
{
&quot;id&quot;: &quot;Practitioner-021600556514&quot;,
&quot;meta&quot;: {
&quot;profile&quot;: [
&quot;https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Practitioner-1&quot;
]
},
&quot;resourceType&quot;: &quot;Practitioner&quot;,
&quot;identifier&quot;: [
{
&quot;system&quot;: &quot;http://fhir.nhs.net/Id/sds-user-id&quot;,
&quot;value&quot;: &quot;021600556514&quot;
}
]
},
{
&quot;id&quot;: &quot;Patient-9462979626&quot;,
&quot;meta&quot;: {
&quot;profile&quot;: [
&quot;https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Patient-1&quot;
]
},
&quot;resourceType&quot;: &quot;Patient&quot;,
&quot;identifier&quot;: [
{
&quot;system&quot;: &quot;http://fhir.nhs.net/Id/nhs-number&quot;,
&quot;value&quot;: &quot;9462979626&quot;
}
]
},
{
&quot;id&quot;: &quot;Patient-1000000001&quot;,
&quot;meta&quot;: {
&quot;profile&quot;: [
&quot;https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Patient-1&quot;
]
},
&quot;resourceType&quot;: &quot;Patient&quot;,
&quot;identifier&quot;: [
{
&quot;system&quot;: &quot;http://fhir.nhs.net/Id/nhs-number&quot;,
&quot;value&quot;: &quot;1000000001&quot;
}
]
}
],
&quot;status&quot;: &quot;current&quot;,
&quot;mode&quot;: &quot;snapshot&quot;,
&quot;entry&quot;: [
{
&quot;extension&quot;: [
{
&quot;extension&quot;: [
{
&quot;url&quot;: &quot;priority&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-Priority-1&quot;,
&quot;code&quot;: &quot;ROUTINE&quot;
}
]
}
},
{
&quot;url&quot;: &quot;specialty&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;_baseUrl_/STU3/CodeSystem/SPECIALTY&quot;,
&quot;code&quot;: &quot;CARDIOLOGY&quot;
}
]
}
},
{
&quot;url&quot;: &quot;patient&quot;,
&quot;valueReference&quot;: {
&quot;reference&quot;: &quot;#Patient-9462979626&quot;
}
},
{
&quot;url&quot;: &quot;eReferralPathwayStart&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-03T14:09:15.297Z&quot;
},
{
&quot;url&quot;: &quot;clinicalInfoPrinted&quot;,
&quot;valueBoolean&quot;: false
},
{
&quot;url&quot;: &quot;requestContextStatus&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-RequestContextStatus-1&quot;,
&quot;code&quot;: &quot;NEVER_REVIEWED&quot;,
&quot;display&quot;: &quot;Never Reviewed&quot;
}
]
}
},
{
&quot;url&quot;: &quot;clinicalInfoFirstSubmitted&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-03T14:09:15.297Z&quot;
},
{
&quot;url&quot;: &quot;referralType&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-ReferralType-1&quot;,
&quot;code&quot;: &quot;TRIAGE_DEFERRAL&quot;
}
]
}
},
{
&quot;url&quot;: &quot;service&quot;,
&quot;valueReference&quot;: {
&quot;identifier&quot;: {
&quot;system&quot;: &quot;http://fhir.nhs.net/Id/ers-service&quot;,
&quot;value&quot;: &quot;11012&quot;
},
&quot;display&quot;: &quot;Excellent Cardiology Business Service 12&quot;
}
}
],
&quot;url&quot;: &quot;https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1&quot;
}
],
&quot;item&quot;: {
&quot;reference&quot;: &quot;ReferralRequest/000000070002&quot;
}
},
{
&quot;extension&quot;: [
{
&quot;extension&quot;: [
{
&quot;url&quot;: &quot;priority&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-Priority-1&quot;,
&quot;code&quot;: &quot;ROUTINE&quot;
}
]
}
},
{
&quot;url&quot;: &quot;specialty&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;_baseUrl_/STU3/CodeSystem/SPECIALTY&quot;,
&quot;code&quot;: &quot;CARDIOLOGY&quot;
}
]
}
},
{
&quot;url&quot;: &quot;patient&quot;,
&quot;valueReference&quot;: {
&quot;reference&quot;: &quot;#Patient-1000000001&quot;
}
},
{
&quot;url&quot;: &quot;allocatedClinician&quot;,
&quot;valueReference&quot;: {
&quot;reference&quot;: &quot;#Practitioner-021600556514&quot;
}
},
{
&quot;url&quot;: &quot;eReferralPathwayStart&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-03T14:09:11.381Z&quot;
},
{
&quot;url&quot;: &quot;clinicalInfoPrinted&quot;,
&quot;valueBoolean&quot;: false
},
{
&quot;url&quot;: &quot;requestContextStatus&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-RequestContextStatus-1&quot;,
&quot;code&quot;: &quot;NEVER_REVIEWED&quot;,
&quot;display&quot;: &quot;Never Reviewed&quot;
}
]
}
},
{
&quot;url&quot;: &quot;clinicalInfoFirstSubmitted&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-03T14:09:11.022Z&quot;
},
{
&quot;url&quot;: &quot;clinicalInfoLastUpdated&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-03T14:09:12.573Z&quot;
},
{
&quot;url&quot;: &quot;appointmentStart&quot;,
&quot;valueDateTime&quot;: &quot;2021-09-05T15:09:03.971Z&quot;
},
{
&quot;url&quot;: &quot;referralType&quot;,
&quot;valueCodeableConcept&quot;: {
&quot;coding&quot;: [
{
&quot;system&quot;: &quot;https://fhir.nhs.uk/STU3/CodeSystem/eRS-ReferralType-1&quot;,
&quot;code&quot;: &quot;APPOINTMENT&quot;
}
]
}
}
],
&quot;url&quot;: &quot;https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1&quot;
}
],
&quot;item&quot;: {
&quot;reference&quot;: &quot;ReferralRequest/000000070001&quot;
}
}
]
}

Currently I've got this.

 var response =  await _fhirClient.OperationAsync(new Uri(EndPointUrl + &quot;FHIR/STU3/ReferralRequest&quot;), &quot;ers.fetchworklist&quot;, paramResource);
var eReferals = new List&lt;eReferral&gt;();
if (response != null)
{
var patientNode = FhirJsonNode.Parse(await response.ToJsonAsync());
var use = patientNode.Children(&quot;entry&quot;).Children(&quot;extension&quot;).ToList();
foreach(var x in use)
{
eReferals.Add(new eReferral
{
Priority = x[&quot;extension[0]&quot;]
})
}
}
return eReferals;

However I don't know how to referance and get the specific elements e.g "url" : "priority" and get the object at that point.

I might be doing this all wrong and there is a much better way to get the various elements however I've been round the block trying to do this - with a few methods and I'm non the wiser at the end.

答案1

得分: 1

您的操作调用结果是一个包含(引用)ReferralRequest资源的List资源。每个条目似乎都有一个包含有关转诊请求的更多详细信息的复杂扩展,并且根据您的问题/示例,看起来您希望创建具有ReferralRequest的引用值以及扩展中的Patient、专科和优先级详情的eReferral对象。

如果这是正确的假设,我建议与List资源结构一起工作,而不是将其转换为json,然后再转换回结构。当您了解List模型时,可以访问其属性,包括扩展。以下是我提取您提到的值的示例:

List list = await _fhirClient.OperationAsync(new Uri(EndPointUrl + &quot;FHIR/STU3/ReferralRequest&quot;), &quot;ers.fetchworklist&quot;, paramResource);

if (list != null)
{
    foreach (List.EntryComponent e in list.Entry)
    {
        Extension mainExtension = e.GetExtension(&quot;https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1&quot;);

        eReferals.Add(new eReferral
        {
            Reference = e.Item.Reference,
            Priority = mainExtension.GetExtensionValue&lt;CodeableConcept&gt;(&quot;priority&quot;).Coding.First().Code,
            Specialty = mainExtension.GetExtensionValue&lt;CodeableConcept&gt;(&quot;specialty&quot;).Coding.First().Code,
            Patient = mainExtension.GetExtensionValue&lt;ResourceReference&gt;(&quot;patient&quot;).Reference
        }) ;
    }
}

我还想补充一句,但不知道您是否对此有任何控制:似乎对于Patient资源,NHS号码已集成到技术ID中。将个人身份信息添加到FHIR资源的技术标识中是不鼓励的。

英文:

Your operation call results in a List resource, where the entries are (references to) ReferralRequest resources. Each entry seems to have a complex extension with more details about the referral request, and from your question/example it looks like you want to create eReferral objects that have the reference value of the ReferralRequest as well as the details for the Patient, specialty and priority from the extension.

If that is the correct assumption, what I would advice is to work with the List resource structure, instead of converting it to json and then back to a structure. When you know the List model, you can access its properties including the extensions. Here's an example where I extract the values that you mentioned:

List list = await _fhirClient.OperationAsync(new Uri(EndPointUrl + &quot;FHIR/STU3/ReferralRequest&quot;), &quot;ers.fetchworklist&quot;, paramResource);

if (list != null)
{
    foreach (List.EntryComponent e in list.Entry)
    {
        Extension mainExtension = e.GetExtension(&quot;https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1&quot;);

        eReferals.Add(new eReferral
        {
            Reference = e.Item.Reference,
            Priority = mainExtension.GetExtensionValue&lt;CodeableConcept&gt;(&quot;priority&quot;).Coding.First().Code,
            Specialty = mainExtension.GetExtensionValue&lt;CodeableConcept&gt;(&quot;specialty&quot;).Coding.First().Code,
            Patient = mainExtension.GetExtensionValue&lt;ResourceReference&gt;(&quot;patient&quot;).Reference
        }) ;
    }
}

I do want to add an extra remark, but do not know if you have any control over this: it seems that for the Patient resources, the NHS number is integrated into the technical id. This kind of adding PII to technical identities of FHIR resources is highly discouraged.

huangapple
  • 本文由 发表于 2023年7月4日 22:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定