# Parsing array of objects

**URL:** <https://discourse.nodered.org/t/parsing-array-of-objects/33126>\
**Category:** General\
**Created:** [20 September 2020 20:15 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126 "2020-09-20T20:15:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 September 2020 20:15 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/1 "2020-09-20T20:15:16Z")

</div>

Anyone help with extracting data from an array of objects.

```auto
{
   "length":6792,
   "data":[
      {
         "fruit":"Apples",
         "id":32827,
         "sales":[
            {
               "week":5,
               "value":234
            },
            {
               "week":6,
               "value":278
            },
            {
               "week":7,
               "value":199
            }
         ]
      },
      {
         "fruit":"Pears",
         "id":78736,
         "sales":[
            {
               "week":5,
               "value":178
            },
            {
               "week":6,
               "value":345
            },
            {
               "week":7,
               "value":286
            }
        ]
      }
   ]
}

```

I reality, there are many hundreds of objects in the array, and much more complex data in the objects, so the above is the example format.

I'd like to;

Select an object where `fruit == Pears` (all of the names are unique - there's only 1 'Pears')  
then be able assign a var to the `sales` id, and finally use the `sales` data to plot a chart.  
I don't yet know the format of the chart, but if I can get 'sales', then I can sort the format later.

Basically, I'm wanting to just select & use one object, selected by the simple criteria `fruit == Pears`.

I seem to remember that that there is a better way than iterating over the array... was it `map`?  
...but can't seem to work it out.

Thanks

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [20 September 2020 20:24 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/2 "2020-09-20T20:24:54Z")

</div>

Hi Paul,

Here a jsonata expression:

- [https://try.jsonata.org/-IucC2PIs](https://try.jsonata.org/-IucC2PIs)

```auto
{
    "var" : payload.data[fruit=$$.selected_fruit].id,
    "sales" : payload.data[fruit=$$.selected_fruit].sales
}

```

So it is expecting as input:

- msg.payload = the array of objects
- msg.selected\_fruit = the name of the fruit you want to select.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 September 2020 21:49 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/3 "2020-09-20T21:49:48Z")

</div>

Great, that's exactly what I need! Thank you

I also like the way that the criteria is included in the msg, because I'm intending to use a dropdown for users to select the criteria, and that makes it easy to implement.

One last question please @janvda, using the apples & pears as the example...  
The code for 'no sales' is `"value":-9999"` (and not `"value":0`) 😡  
Can we change all of the -9999 numbers to 0, where the key is "value" (within the selected object) so it's easy to present in a chart?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [21 September 2020 05:58 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/4 "2020-09-21T05:58:22Z")

</div>

> [@Paul-Reed](#):
>
> Can we change all of the -9999 numbers to 0, where the key is "value" (within the selected object) so it's easy to present in a chart?

there are many ways to do this.  
One way (not most performant one!).

```auto
{
    "var" : payload.data[fruit=$$.selected_fruit].id=-9999?0:payload.data[fruit=$$.selected_fruit].id,
    "sales" : payload.data[fruit=$$.selected_fruit].sales
}

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [21 September 2020 08:52 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/5 "2020-09-21T08:52:02Z")

</div>

@janvda I'm afraid that doesn't work.  
I've tried applying the `-9999?0` to sales (which is the array containing the value), but still not working.  
[https://try.jsonata.org/ruDaeHprb](https://try.jsonata.org/ruDaeHprb)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [21 September 2020 09:17 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/6 "2020-09-21T09:17:00Z")

</div>

> [@Paul-Reed](#):
>
> I'm afraid that doesn't work.

You are right. My fault. I have misunderstood your request by reading it too quickly.

The following expression should work (I hope):

```auto
{
    "var" : payload.data[fruit=$$.selected_fruit].id,
    "sales" : payload.data[fruit=$$.selected_fruit].sales.{
        "value" : value=-9999?0:value,
        "week" : week
    }
}

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [21 September 2020 09:56 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/7 "2020-09-21T09:56:41Z")

</div>

> [@janvda](#):
>
> The following expression should work (I hope)

It does indeed!  
Thank you for your help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 October 2020 09:56 UTC](https://discourse.nodered.org/t/parsing-array-of-objects/33126/8 "2020-10-05T09:56:47Z")

</div>

Closed on 5 October 2020 09:56 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
