trackedEntityInstances endpoint GET request

Hello, I’m building a custom app for my organization to help interact with some DHIS2 data.

I would like help in identifying how to slim down the .json response fields when putting in a GET request to only those which I need. I actually only need specific attributes to the TEI so what do I need in include to only have that which I’d like?


(This is dummy data, not sensitive info) This is the response I get when making a call with this …

http://url/api/trackedEntityInstances.json?program=kpauVkGAK8z&ou=yM3rUZC0Hvl

I’m only interested in the attributes, and not all attributes, 3 out of the 5. What do I need to add to my api call?

Also, making the same request with the new tracker endpoint, and using attribute parameter in the call to specify the attributes, returns more or less the same response with the fields I’m looking to get rid of in the response.

http://xxxx/api/tracker/trackedEntities?trackedEntityType=QZnlVF9iSlD&orgUnit=yM3rUZC0Hvl&attribute=osf4j0yRYlm&attribute=p1NB7CSm10L

What am I not getting right?

1 Like

Hi @lillian1n2,

Good luck with your custom application! Please try the fields parameter. For example, https://play.dhis2.org/2.35.14/api/trackedEntityInstances?ou=ImspTQPwCqd&progam=IpHINAT79UW&fields=attributes[displayName,value]

Field parameter will allow you to select the attributes that you want to appear in the response, read more here: https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_metadata_field_filter


If you want a good review, in this video @austin explains queries and how to use the filter and fields. Web App Development Workshop - Day 2 - Queries and mutations - YouTube

If you haven’t you might be interested in checking out the latest 2022 DHIS2 Web App Development Academy playlist: DHIS2 Web App Development Academy 2022 - YouTube

1 Like

Hi @lillian1n2,

Sorry my previous post didn’t touch the point exactly. The request and your use of the attribute parameter is correct but since it’s not returning the expected result it might be a bug, so would you like to create a jira bug issue (in jira.dhis2.org)?

Thanks!

2 Likes

Thank you for the response. I will endeavor to raise the issue.

Also thank you for pointing me to the webinars on app development and I had actually had a look at those resources when preparing to undertake the project. They helped with my understanding of how to work with the API.

Though the resources that were more specific to the work I’m trying to do with the app is this:
https://dhis2trainingland.com/eportal/?tutorial=linking-dhis2-to-an-external-web-portal-using-php-mysql

Because it’s giving me ideas of how to have my code interact with the data retrieved via API.

1 Like

Hi Gassim,

Follow up to your response here about using the fields parameter to query particular data,

According to the documentation, I can query a field by its attribute ID and it would give me the value

" Example: Get id, name and the value of a user defined attribute with ID DnrLSdo4hMl for organisation units:
/api/organisationUnits?fields=id,name,DnrLSdo4hMl
The attribute is then included as property DnrLSdo4hMl of each matching object in the response. This can be renamed using the rename transformer as shown in the next section."

Following this, I have tried to build my query with the attribute IDs I’m interested in for the attributes of the TEI, and I am not receiving the desired response

http://xxxx/api/trackedEntityInstances?trackedEntityType=QZnlVF9iSlD&ou=yM3rUZC0Hvl&fields=trackedEntityInstance,attributes[osf4j0yRYlm]

I would like this to query a specific attribute in the TEI list above i.e. Account No

Any recommendations about how to go about the same?

@lillian1n2 The fields parameter returns the parameters you want in the JSON query.
For the case of querying a specific attribute(not value though), you can use the filter with the metadata filter operators

Could you give me an example? What fields of the attributes array would it return without the value? I’m still getting a handle on the filter parameter when used with the TEI attributes.

Supposed you wanted to get storedBy, displayName attributes only,
you run a query like this http://xxxx/api/trackedEntityInstances?trackedEntityType=QZnlVF9iSlD&ou=yM3rUZC0Hvl&fields=trackedEntityInstance,attributes[storedBy, displayName]
And if you want to get only a specific attribute value like displayName=“Account No” or storedBy=“some user”, you would do this; filter the attribute using operators like EQ, LIKE, etc for example
attribute=idOfTheTEIAttribute:EQ:theValueToFilter as shown in
http://xxxx/api/trackedEntityInstances?trackedEntityType=QZnlVF9iSlD&ou=yM3rUZC0Hvl&fields=trackedEntityInstance&attribute=osf4j0yRYlm:EQ:accountNO assuming osf4j0yRYlm is the TEI attribute id for accountNo and other values.

As for the first example, I was able to isolate the field names and that responds as I would want from my end.

As for the second example, this involves querying the attribute parameter which I discovered does not respond as desired. I raised the issue on jira.

So my main issue, is how to select certain attributes, I am not interested in all of them, but specific ones. So I could use the field parameters (and I have) but I only want for 4 out of 6 of the attributes because it’s affecting how I’m building my code for parsing through the response array in JSON.

I thought the attribute query would work, but it isn’t responding as I need it to. With fields, it slims down the response to the fields I am interested in, however, I still need to find out how to select specific attributes of the list.

@Gassim Question: After raising the issue on jira, what can I expect after for resolution of the issue? Especially also considering I am doing the development on 2.37.7.1 for which I don’t know whether there will be further patches/updates.