Stream: fhirpath
Topic: constructing arrays or objects
Tim Harsch (Jun 30 2020 at 03:37):
Hello, I am trying to construct a result set from a fhirpath query on data that looks like this:
image.png
Optimally, I want a JSON structure back that looks like
[
{
'provider': 'HAPI Public Server - Public HAPI Server'
'resource': record_body.entry.resource
},
{...}
]
The closest I've gotten is this query:
provider.union(record_body.entry.resource.where(resourceType = 'Observation'))
I'm expecting it might return the provider and the resource object, e.g.:
[
'HAPI Public Server - Public HAPI Server',
record_body.entry.resource,
'HAPI Public Server - Public HAPI Server',
record_body.entry.resource,
...
]
In fhirpath,js this yields
Error: Not implemented: union
at doInvoke (/Users/harsch/git/patient-portal/patient-web/node_modules/fhirpath/src/fhirpath.js:459:11)
at Object.<anonymous>.engine.FunctionInvocation (/Users/harsch/git/patient-portal/patient-web/node_modules/fhirpath/src/fhirpath.js:501:10)
at Object.<anonymous>.engine.doEval (/Users/harsch/git/patient-portal/patient-web/node_modules/fhirpath/src/fhirpath.js:585:22)
at /Users/harsch/git/patient-portal/patient-web/node_modules/fhirpath/src/fhirpath.js:157:19
So, I guess the crux of my question is: How do you construct arbitrary array or objects with Fhirpath?
Grahame Grieve (Jun 30 2020 at 04:16):
might be a question for #javascript but if you're running in HAPI why are you using the js FHIRPath engine not the java one?
Paul Lynch (Jun 30 2020 at 14:26):
Tim Harsch said:
In fhirpath,js this yields
Error: Not implemented: union
In fhirpath.js, the "union" function is not yet implemented, but the union operator "|" is, so you should be able to write:
provider | record_body.entry.resource.where(resourceType = 'Observation')
Bryn Rhodes (Jun 30 2020 at 14:53):
You can use the .select()
function to shape the results, but there is no generic object construction syntax in FHIRPath, and the array construction syntax is limited to the use of union.
Tim Harsch (Jun 30 2020 at 15:42):
Grahame Grieve said:
might be a question for #javascript but if you're running in HAPI why are you using the js FHIRPath engine not the java one?
Thanks for the response. I posted here because I'm only concerned with query syntax at the moment. I suspect the not implemented
error will not be an issue if I find the proper query.
HAPI is just one provider I'm fetching data from. My code is intended for a client processing the data from the provider.
Tim Harsch (Jun 30 2020 at 15:49):
Bryn Rhodes said:
You can use the
.select()
function to shape the results, but there is no generic object construction syntax in FHIRPath, and the array construction syntax is limited to the use of union.
Hmm... my original query that served me well for some time was this: provider | record_body.entry.resource.where(resourceType = '${type}')
(an ecmascript template string, $type is a JS var). which yields an array with providername and N resource objects. When I had a second provider in my data I would get 2 providers, followed by N resources from the first, and M resources from the second (screenshot below). So I'm unable to tie the providers back to resources. Can you think of a way to handle this without object construction?
Paul Lynch (Jun 30 2020 at 18:00):
I haven't tested it, but what about something using .select as Bryn suggested, like: select( provider | $this.entry.....)
Tim Harsch (Jun 30 2020 at 19:49):
I tried something like this: select( $this.provider | $this.record_body.entry.resource.where(resourceType = 'Observation'))
but an issue I see is that providers show up for records that are not Observations as in:
image.png
The data file is here: https://gist.github.com/timharsch/05d5db2d690e486c38f476fa424a1f98
Tim Harsch (Jun 30 2020 at 19:53):
It's almost like http://hl7.org/fhirpath/N1/#string-concatenation if there were an & operator I could use in place of pipe.
Paul Lynch (Jun 30 2020 at 21:56):
I guess you are wanting to filter out the bundles that don't have Observations. Before you do the select, you could do a where: where(record_body.entry.resource.resourceType.first() = 'Observation').select(...). I am assuming each bundle only has resources of one type.
Last updated: Apr 12 2022 at 19:14 UTC