Assistance: API call - Nested query

Hi all,

For my tracker program, I would like to query for Tracked Entity attributes (name, age, gender, etc) and data elements of a specific program stage or program in the same request. I have read pages about Event query analytics in the Developer Manual, but I couldn’t get what want.

I am wondering if you help me with a nested web api code to retrieve TE attributes, data elements, and option sets.

Thanks in advance

Hey @dmbantu,
I will share with you an example from the play.dhis2.org instance. First here’s the link: https://play.dhis2.org/2.37.0/api/trackedEntityInstances?fields=attributes[displayName,value]&ou=DiszpKrYNg8&program=IpHINAT79UW&pageSize=50&page=1&totalPages=false you’d have to be logged in to DHIS 2 Demo - Sierra Leone to see it.

First, there are two main requirements to get the TEI: OU & program (and you could get their IDs from the Maintenance app), and then for your request I used the fields parameter to choose what I want to be displayed which in this case is attributes. I could have chosen to just keep it as fields=attributes; however, if I don’t want to see all the details such as “lastUpdated”…etc then I could specify what I want in closed brackets such as fields=attributes[displayName,value]. You could start with fields=* to all the possible selections and then choose the info you want.

For example, in addition to displaying the attributes, I could display the status of the enrollment:
https://play.dhis2.org/2.37.0/api/trackedEntityInstances?fields=enrollments[status],attributes[displayName,value]&ou=DiszpKrYNg8&program=IpHINAT79UW&pageSize=50&page=1&totalPages=false and all I did was add enrollments[status]

I hope this clarifies a bit please let me know if you want any additional info! Thanks! (:

Hi @Gassim,

The shared APIs do really help. But what about if I want to display, a part from attributes, data elements values of a specific program stage or all program stages in the same API. is this possible?

Thanks again.

Then you’d have to use the “programs” resource (to check all available resources, click here) and I have not tested using more than one resource in the same API URL request, but it’s possible for App developers to create such queries.

To see all available info you could get from the programs resource use fields=* such as:
https://play.dhis2.org/2.37.0/api/programs?fields=* or for a specific program https://play.dhis2.org/2.37.0/api/programs/IpHINAT79UW?fields=*

To specify the data you want for example all the program stages in a program:
https://play.dhis2.org/2.37.0/api/programs/IpHINAT79UW?fields=id,displayName,programStages

Similarly, for programStageDataElements:
https://play.dhis2.org/2.37.0/api/programStages?fields=displayFormName,programStageDataElements

But if you want info about the Data Elements then you’d have to use the DE resource (but make sure to filter the domainType to Tracker so that you only get the ones linked to programs):
https://play.dhis2.org/2.37.0/api/dataElements?fields=displayName,description&filter=domainType:eq:TRACKER&pageSize=5

Sounds to me like you’re describing a sqlView query that you could build and then execute? I’m no expert in sql, but that’s where I’d be going—in essence it sounds like you want to join two + tables together within a single request.

1 Like

Hi @Gassim,

I will go through the APIs you have shared and get back.

Thanks

1 Like

Hi @Matthew_Boddie,

Using SQl for querying the data is also a good way to go. unfortunately, I need to explore it first. Anything would you like to share about SQL views?

Tnanks