Accessing Data from object that is in array(arrays again in object)

I am using http request to get data of one month. Screenshots of what I am getting in debug is attached below which is array of objects. Now I want to access minvalue from the first object and maxvalue from last object. But I am unable to write the function node for it.
Please help me with this.

[{"id":"46b3ab03.36e904","type":"ui_button","z":"88215ed2.1446d","name":"","group":"ee1c959c.6ac4f8","order":0,"width":0,"singleBrowser":false,"height":0,"passthru":false,"label":"This Month","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","powerMode":false,"x":290,"y":760,"wires":[["4a655ce9.ac06d4"]]},{"id":"4a655ce9.ac06d4","type":"http request","z":"88215ed2.1446d","name":"","method":"GET","ret":"obj","url":"","timeout":"","mindspherePath":"/api/iottsaggregates/v4/aggregates?assetId=ded1a0bfec43400f970e566f8263c6d3&aspectName=BatchMix&from=2022-12-01T00:00:00.000Z&to=2022-12-07T00:00:00.000Z&intervalUnit=hour&intervalValue=12&select=Agg1_Current_Weight","useMindsphereAuth":true,"isAdmin":false,"secretHeaders":"","x":530,"y":760,"wires":[["dc97188a.f2fe58","aacbcb9e.ef42a8"]]},{"id":"aacbcb9e.ef42a8","type":"function","z":"88215ed2.1446d","name":"","func":"const agg1 = msg.payload[0];\nconst minvalue = agg1.Agg1_Total_Weight.minvalue;\n//const maxvalue = agg1.maxvalue;\n//const difference = maxvalue - minvalue;\nmsg.payload = minvalue;\nreturn msg;\n//return { payload: { minvalue, maxvalue, difference } };","outputs":1,"language":"javascript","noerr":0,"x":740,"y":760,"wires":[["b8e08189.44e68"]],"_type":"node"},{"id":"b8e08189.44e68","type":"ui_text","z":"88215ed2.1446d","group":"ee1c959c.6ac4f8","order":0,"width":0,"singleBrowser":false,"height":0,"name":"","label":"Agg1","format":"{{msg.payload}}","layout":"row-left","x":930,"y":760,"wires":[]},{"id":"dc97188a.f2fe58","type":"debug","z":"88215ed2.1446d","name":"","active":true,"console":"false","xaxis":"_time","complete":"false","x":630,"y":880,"wires":[]},{"id":"ee1c959c.6ac4f8","type":"ui_group","z":"","name":"This Month","tab":"975e5c7e.bc29e","order":4,"disp":true,"width":"6","collapse":false,"_type":"config"},{"id":"975e5c7e.bc29e","type":"ui_tab","z":"","name":"Test Plant","icon":"dashboard","publicAccess":"true","fmAccess":"none","fmAccessType":"private","assetType":"","asset":"","assetName":"","assetText":"","image":"","description":"","hideTitleBar":false,"_type":"config"}]

Screenshot (119)

Just like your other topic, the message arrives as msg and not as msg.payload (how did you accomplish this?)

if there is no .payload:

const agg1 = msg.payload[0] this does not work.

It works with msg.payload.

Yesterday, Solution (by @tve) which worked is as below

const agg1 = msg.payload[0].Agg1_Total_Weight;
const minvalue = agg1.minvalue;
const maxvalue = agg1.maxvalue;
const difference = maxvalue - minvalue;
msg.payload = difference;
return msg;

I refer to your screenshot.

Yes but, I think it represents msg.payload only.
That soluation works for array of single object but gives error for more then one objects. I think I have missed something. I guess here It's object in arrays and arrays are again in one object.
Error: Cannot read properties of undefined (reading 'Agg1_Total_Weight') (at position 1:49)

Yes but, I think it represents msg.payload only.

It states: msg: Object
and the properties live directly under it. There is no payload.

So I should write
const agg1 = msg[0].Agg1_Total_Weight; instead of const agg1 = msg.payload[0].Agg1_Total_Weight;
do I understand correct?

The debug panel is your friend here.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.


1 Like

I am getting 1st object from the object of arrays. But now not able to get the of particular member of that object (For exp maxvalue minvalue....).
Please help me with this!

flows (7).json (2.3 KB)

Where do you get the data from, via an http request node ?
If yes, the aggregates array can never end up like msg.aggregates without some manual manipulation.

yes. via http request node.

That is not possible in standard node-red.
Then again, I see you posted a screenshot of a flow that does not look like a standard node-red installation.

Did you go over the documentation that @jbudd linked to and explained ?

It's working!
Well, it was quite easy. Sorry for bothering and thank you for patience and help.

const agg1 = msg.payload.aggregates[0].Agg1_Total_Weight.minvalue;
msg.payload = agg1;
return msg;