API to get data elements from DE Group

Hi, everyone.

I am looking for an API to fetch data elements Ids and Names from DE Groups.

Thanks

Would something like https://XXX/api/dataElements?filter=dataElementGroups.id:like:YYY

Work? XXX is the original URL and YYY is the uid of the data Element Group that you are looking for.

Hi, @Matthew_Boddie,

It works, but is it possible to fetch data elements from many or all Groups at the same time?

Thanks

Hi Ferdinand,

You can pull the list of dataElementGroups that you want using the API, then in the list of fields that you’re pulling, you can include the subset of all dataElements attached to each dataElementGroup using square brackets. Eg:

https://play.im.dhis2.org/stable-2-41-0-1/api/dataElementGroups?fields=id,name,dataElements[id,name]

If you want to pull all dataElementGroups, don’t forget to add “&paging=false”.

Cheers, Sam.

2 Likes

Hi @SamuelJohnson,

In my case I am interested in getting program indicators from all program indicator groups. How would the API look like?

Thanks

Hi Davuya,

You can use exactly the same principle. First of all, in your browser, just get the API to list the parent objects that you want (eg programIndicatorGroups), specifying that you want all fields. This will let you know what fields are available:

[your DHIS2 URL]/api/programIndicatorGroups?fields=:all

If you look carefully, you’ll see that there are a couple of collections nested in this list of fields - in this case, you’ll see a collection called “programIndicators”, with an indented list of IDs - these are the programIndicators attached to that specific programIndicatorGroup:

  "programIndicators": [
    {
      "id": "Thkx2BnO5Kq"
    },
    {
      "id": "XCMi7Wvnplm"
    },
    {
      "id": "Y7hKDSuqEtH"
    },
    {
      "id": "x7PaHGvgWY2"
    },
    {
      "id": "hlPt8H4bUOQ"
    }

So now you can just do the same API call again, but instead of requesting all fields, just request that programIndicators field, and specify (within square brackets) the information that you want about these programIndicators:

[your DHIS2 URL]/api/programIndicatorGroups?fields=programIndicators[id,name,aggregationType,program]

Note that you can then dig further to find out the sub-fields of any of fields above - for example, if you want the name instead of the ID of the program that the programIndicator belongs to, you can just add more square brackets:

[Your DHIS2 URL]/api/programIndicatorGroups?fields=programIndicators[id,name,aggregationType,program[id,name]]

Cheers, Sam.

2 Likes

Hi @SamuelJohnson,

All APIs perform well, but to get all program indicators I added page=false. What I can not get from the following API is the Program Indicator group Name and ID. What is missing?
api/29/programIndicatorGroups?paging=false&fields=programIndicators[id,name,aggregationType,program[id,name]]

Thanks again

Hi Davuya,

Just add “id,name,” to the start of the list of “fields=” - see the URL I shared with Ferdinand as an example.

If you’ve done the first step I suggested to you above (which will return all fields available for programIndicatorGroups), you can then add any of those fields to your “fields=” statement.

Cheers, Sam.

1 Like

Hi @SamuelJohnson,

I got the desired result after adding id and name to the fields statement.

Thank you so much.