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?
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?
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.
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?
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.
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?
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.
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?
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?
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)
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)