Parsing the json output from the API

Below is a patient tracked entity instance

In order to get a first name or a last name from this attribute list, we would have to loop through all the attributes until we find an attribute that has the displayName “firstName”, as all the attribute seem to be in random order for each patient. That is inefficient and clunky. Is there a way to get the data in a format that could be read by javascript more easily?

{
“lastUpdated”:“2014-03-28 12:33:47.354”,
“trackedEntity”:“G0PlBqu2Boe”,
“created”:“2014-03-26 15:46:02.45”,
“orgUnit”:“DiszpKrYNg8”,
“trackedEntityInstance”:“JRm9etA1dG1”,
“relationships”:[

     ],
     "attributes":[
        { 
           "code":"MMD_PER_NAM",
           "displayName":"First name",
           "valueType":"TEXT",
           "attribute":"w75KJ2mc4zz",
           "value":"Kedija"
        },
        { 
           "displayName":"Gender",
           "valueType":"TEXT",
           "attribute":"cejWyOfXge6",
           "value":"Female"
        },


]
},

I tried something like the below, but that doesn’t seem to work. What am I missing?

https://play.dhis2.org/demo/api/trackedEntityInstances/PQfMcpmXeFE.json?fields=attributes[displayName]&filter=attributes[displayName]:eq:Gender

Hi Knut,

What are you using for parsing JSON (which library)?

In general you should never rely on order of items in JSON. It is better to interpret it as a “map” rather than “list”.

Best Regards,

Araz

···

On Sun, Nov 22, 2015 at 7:32 AM, Knut Staring knutst@gmail.com wrote:

Below is a patient tracked entity instance

In order to get a first name or a last name from this attribute list, we would have to loop through all the attributes until we find an attribute that has the displayName “firstName”, as all the attribute seem to be in random order for each patient. That is inefficient and clunky. Is there a way to get the data in a format that could be read by javascript more easily?

{
“lastUpdated”:“2014-03-28 12:33:47.354”,
“trackedEntity”:“G0PlBqu2Boe”,
“created”:“2014-03-26 15:46:02.45”,
“orgUnit”:“DiszpKrYNg8”,
“trackedEntityInstance”:“JRm9etA1dG1”,
“relationships”:[

     ],
     "attributes":[
        { 
           "code":"MMD_PER_NAM",
           "displayName":"First name",
           "valueType":"TEXT",
           "attribute":"w75KJ2mc4zz",
           "value":"Kedija"
        },
        { 
           "displayName":"Gender",
           "valueType":"TEXT",
           "attribute":"cejWyOfXge6",
           "value":"Female"
        },


]
},

I tried something like the below, but that doesn’t seem to work. What am I missing?

https://play.dhis2.org/demo/api/trackedEntityInstances/PQfMcpmXeFE.json?fields=attributes[displayName]&filter=attributes[displayName]:eq:Gender


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Hey Knut, Araz,

The order for arrays is guaranteed according to the JSON standard i believe. However for the objects property order is not. (Just like in Javascript)

But we (DHIS2) don’t seem to always return the array in the same order from the server side, so in that case you can’t really trust the order like you said.

Rework the list into a Map(JS Object) like Araz says will work. I just coded you up a small function that does the transformation. See the following gist link.

https://gist.github.com/Markionium/0e55e774b3c554521040#file-transformattributes-js

Regards,

Mark

···

On Sun, Nov 22, 2015 at 10:08 AM, Araz Abishov araz.abishov.gsoc@gmail.com wrote:

Hi Knut,

What are you using for parsing JSON (which library)?

In general you should never rely on order of items in JSON. It is better to interpret it as a “map” rather than “list”.

Best Regards,

Araz


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

On Sun, Nov 22, 2015 at 7:32 AM, Knut Staring knutst@gmail.com wrote:

Below is a patient tracked entity instance

In order to get a first name or a last name from this attribute list, we would have to loop through all the attributes until we find an attribute that has the displayName “firstName”, as all the attribute seem to be in random order for each patient. That is inefficient and clunky. Is there a way to get the data in a format that could be read by javascript more easily?

{
“lastUpdated”:“2014-03-28 12:33:47.354”,
“trackedEntity”:“G0PlBqu2Boe”,
“created”:“2014-03-26 15:46:02.45”,
“orgUnit”:“DiszpKrYNg8”,
“trackedEntityInstance”:“JRm9etA1dG1”,
“relationships”:[

     ],
     "attributes":[
        { 
           "code":"MMD_PER_NAM",
           "displayName":"First name",
           "valueType":"TEXT",
           "attribute":"w75KJ2mc4zz",
           "value":"Kedija"
        },
        { 
           "displayName":"Gender",
           "valueType":"TEXT",
           "attribute":"cejWyOfXge6",
           "value":"Female"
        },


]
},

I tried something like the below, but that doesn’t seem to work. What am I missing?

https://play.dhis2.org/demo/api/trackedEntityInstances/PQfMcpmXeFE.json?fields=attributes[displayName]&filter=attributes[displayName]:eq:Gender


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Personally I wouldn’t create the map based on names, since they could potentially change, better to use the ID.

Simplest way of doing this I can think of is using lodash (which you probably use anyways) and do:

const groupedById = _.groupBy(o.attributes, “id”);

···

On Sun, Nov 22, 2015 at 7:00 PM, Mark Polak mark@thedutchies.com wrote:

Hey Knut, Araz,

The order for arrays is guaranteed according to the JSON standard i believe. However for the objects property order is not. (Just like in Javascript)

But we (DHIS2) don’t seem to always return the array in the same order from the server side, so in that case you can’t really trust the order like you said.

Rework the list into a Map(JS Object) like Araz says will work. I just coded you up a small function that does the transformation. See the following gist link.

https://gist.github.com/Markionium/0e55e774b3c554521040#file-transformattributes-js

Regards,

Mark


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp


Morten

On Sun, Nov 22, 2015 at 10:08 AM, Araz Abishov araz.abishov.gsoc@gmail.com wrote:

Hi Knut,

What are you using for parsing JSON (which library)?

In general you should never rely on order of items in JSON. It is better to interpret it as a “map” rather than “list”.

Best Regards,

Araz


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

On Sun, Nov 22, 2015 at 7:32 AM, Knut Staring knutst@gmail.com wrote:

Below is a patient tracked entity instance

In order to get a first name or a last name from this attribute list, we would have to loop through all the attributes until we find an attribute that has the displayName “firstName”, as all the attribute seem to be in random order for each patient. That is inefficient and clunky. Is there a way to get the data in a format that could be read by javascript more easily?

{
“lastUpdated”:“2014-03-28 12:33:47.354”,
“trackedEntity”:“G0PlBqu2Boe”,
“created”:“2014-03-26 15:46:02.45”,
“orgUnit”:“DiszpKrYNg8”,
“trackedEntityInstance”:“JRm9etA1dG1”,
“relationships”:[

     ],
     "attributes":[
        { 
           "code":"MMD_PER_NAM",
           "displayName":"First name",
           "valueType":"TEXT",
           "attribute":"w75KJ2mc4zz",
           "value":"Kedija"
        },
        { 
           "displayName":"Gender",
           "valueType":"TEXT",
           "attribute":"cejWyOfXge6",
           "value":"Female"
        },


]
},

I tried something like the below, but that doesn’t seem to work. What am I missing?

https://play.dhis2.org/demo/api/trackedEntityInstances/PQfMcpmXeFE.json?fields=attributes[displayName]&filter=attributes[displayName]:eq:Gender


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Like Morten says, lodash.groupBy is probably better, (if you using that already)

The ID might not be the same across instances? So if it needs to work across instances that won’t work?

···

On Sun, Nov 22, 2015 at 1:13 PM, Morten Olav Hansen mortenoh@gmail.com wrote:

Personally I wouldn’t create the map based on names, since they could potentially change, better to use the ID.

Simplest way of doing this I can think of is using lodash (which you probably use anyways) and do:

const groupedById = _.groupBy(o.attributes, “id”);


Morten

On Sun, Nov 22, 2015 at 7:00 PM, Mark Polak mark@thedutchies.com wrote:

Hey Knut, Araz,

The order for arrays is guaranteed according to the JSON standard i believe. However for the objects property order is not. (Just like in Javascript)

But we (DHIS2) don’t seem to always return the array in the same order from the server side, so in that case you can’t really trust the order like you said.

Rework the list into a Map(JS Object) like Araz says will work. I just coded you up a small function that does the transformation. See the following gist link.

https://gist.github.com/Markionium/0e55e774b3c554521040#file-transformattributes-js

Regards,

Mark


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

On Sun, Nov 22, 2015 at 10:08 AM, Araz Abishov araz.abishov.gsoc@gmail.com wrote:

Hi Knut,

What are you using for parsing JSON (which library)?

In general you should never rely on order of items in JSON. It is better to interpret it as a “map” rather than “list”.

Best Regards,

Araz


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

On Sun, Nov 22, 2015 at 7:32 AM, Knut Staring knutst@gmail.com wrote:

Below is a patient tracked entity instance

In order to get a first name or a last name from this attribute list, we would have to loop through all the attributes until we find an attribute that has the displayName “firstName”, as all the attribute seem to be in random order for each patient. That is inefficient and clunky. Is there a way to get the data in a format that could be read by javascript more easily?

{
“lastUpdated”:“2014-03-28 12:33:47.354”,
“trackedEntity”:“G0PlBqu2Boe”,
“created”:“2014-03-26 15:46:02.45”,
“orgUnit”:“DiszpKrYNg8”,
“trackedEntityInstance”:“JRm9etA1dG1”,
“relationships”:[

     ],
     "attributes":[
        { 
           "code":"MMD_PER_NAM",
           "displayName":"First name",
           "valueType":"TEXT",
           "attribute":"w75KJ2mc4zz",
           "value":"Kedija"
        },
        { 
           "displayName":"Gender",
           "valueType":"TEXT",
           "attribute":"cejWyOfXge6",
           "value":"Female"
        },


]
},

I tried something like the below, but that doesn’t seem to work. What am I missing?

https://play.dhis2.org/demo/api/trackedEntityInstances/PQfMcpmXeFE.json?fields=attributes[displayName]&filter=attributes[displayName]:eq:Gender


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Sure, but you don't have any guarantee about anything like that, not name,
age, etc.. nothing is created for you. You need synchronised metadata to
make this work properly across instances (if you actually need to know what
is the name etc, I don't think we have any special handling of that in our
tracker apps, just display associated attributes)

···

On Sun, Nov 22, 2015 at 7:21 PM, Mark Polak <mark@thedutchies.com> wrote:

The ID might not be the same across instances? So if it needs to work
across instances that won't work?

--
Morten

Right, that makes sense. My knowledge about the whole tracker is fairly limited.

I’ll ask Abyot at some point how he handles that :slight_smile:

···

On Sun, Nov 22, 2015 at 1:36 PM, Morten Olav Hansen mortenoh@gmail.com wrote:

On Sun, Nov 22, 2015 at 7:21 PM, Mark Polak mark@thedutchies.com wrote:

The ID might not be the same across instances? So if it needs to work across instances that won’t work?

Sure, but you don’t have any guarantee about anything like that, not name, age, etc… nothing is created for you. You need synchronised metadata to make this work properly across instances (if you actually need to know what is the name etc, I don’t think we have any special handling of that in our tracker apps, just display associated attributes)


Morten

Hi,

I am using ID. Tracker apps do not support across instances. Not sure if we have an app that supports across instances.

Whenever an ID is changed - it is like a new meta-data and the practice so far is to clear cache and download the new meta-data.

···

On Sun, Nov 22, 2015 at 1:56 PM, Mark Polak mark@thedutchies.com wrote:

Right, that makes sense. My knowledge about the whole tracker is fairly limited.

I’ll ask Abyot at some point how he handles that :slight_smile:


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp


Thank you,

Abyot.

On Sun, Nov 22, 2015 at 1:36 PM, Morten Olav Hansen mortenoh@gmail.com wrote:

On Sun, Nov 22, 2015 at 7:21 PM, Mark Polak mark@thedutchies.com wrote:

The ID might not be the same across instances? So if it needs to work across instances that won’t work?

Sure, but you don’t have any guarantee about anything like that, not name, age, etc… nothing is created for you. You need synchronised metadata to make this work properly across instances (if you actually need to know what is the name etc, I don’t think we have any special handling of that in our tracker apps, just display associated attributes)


Morten