← Back to team overview

dhis2-devs team mailing list archive

Re: Parsing the json output from the API

 

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@xxxxxxxxxxxxxxx> 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
>
>
>
> On Sun, Nov 22, 2015 at 10:08 AM, Araz Abishov <
> araz.abishov.gsoc@xxxxxxxxx> 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
>>
>> On Sun, Nov 22, 2015 at 7:32 AM, Knut Staring <knutst@xxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxx
>>> Unsubscribe : https://launchpad.net/~dhis2-devs
>>> More help   : https://help.launchpad.net/ListHelp
>>>
>>>
>>
>> _______________________________________________
>> Mailing list: https://launchpad.net/~dhis2-devs
>> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~dhis2-devs
>> More help   : https://help.launchpad.net/ListHelp
>>
>>
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dhis2-devs
> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dhis2-devs
> More help   : https://help.launchpad.net/ListHelp
>
>

Follow ups

References