Parsing array of objects

Anyone help with extracting data from an array of objects.

{
   "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

Hi Paul,

Here a jsonata expression:

{
    "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.
1 Like

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) :rage:
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!).

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

@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

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

The following expression should work (I hope):

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

It does indeed!
Thank you for your help.

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