lets say in the morning you make the first request to the api .. you get some data back .. each element with a unique session_id ..
later on during the same day you make another request .. you get the same morning data plus some appended data to the array.
so you want with each request to the api to check and get only the new data and also to restructure the data to a more useful format?
it would be good, if you could, to share some real data
here's my attempt .. in msg.payload
you get the unique new data between requests ..
you still need to implement the logic to delete the saved sessionData
from Context .. possibly with an inject node at the end of the day in order to have Context cleared for the next day.
[{"id":"534d3c8475e49cf4","type":"inject","z":"4895ea10b4ee9ead","name":"1st request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[ { \"center_id\":1111, \"name\":\"AAAAA\", \"sessions\":[ { \"session_id\":\"A111\", \"date\":\"19-07-2021\" }, { \"session_id\":\"B111\", \"date\":\"20-07-2021\" }, { \"session_id\":\"C111\", \"date\":\"21-07-2021\" } ] }, { \"center_id\":2222, \"name\":\"BBBB\", \"sessions\":[ { \"session_id\":\"A222\", \"date\":\"19-07-2021\" }, { \"session_id\":\"B222\", \"date\":\"20-07-2021\" } ] }, { \"center_id\":3333, \"name\":\"CCCC\", \"sessions\":[ { \"session_id\":\"C111\", \"date\":\"19-07-2021\" } ] } ]","payloadType":"json","x":290,"y":460,"wires":[["7a098efeb56cc28b","3c670cdc32ad3980"]]},{"id":"7a098efeb56cc28b","type":"function","z":"4895ea10b4ee9ead","name":"","func":"let restructuredData = []\nlet sessionData = flow.get('sessionData')\nlet uniqueData = []\n\n// restructuredData - loop through every element of payload and every el in sessions\nmsg.payload.forEach(el => {\n el.sessions.forEach(session => {\n restructuredData.push({\n center_id: el.center_id,\n name: el.name,\n session_id: session.session_id,\n date: session.date\n })\n })\n})\n\n// check if Context has any previous data saved \nif (sessionData && sessionData.length > 0) {\n\n // make arr of only savedIds to make it easier for some()\n let savedIds = sessionData.map(v => v.session_id)\n // loop through and check every element in new restructuredData if its unique session_id doesnt exist \n restructuredData.forEach(d => {\n if (!savedIds.some(v => v == d.session_id)) {\n uniqueData.push(d)\n }\n })\n sessionData = sessionData.concat(uniqueData)\n // node.warn(uniqueData);\n flow.set(\"sessionData\", sessionData);\n msg.payload = uniqueData;\n return msg;\n\n}\n// no data in context / save first data\nelse {\n flow.set(\"sessionData\", restructuredData); \n msg.payload = restructuredData;\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":520,"wires":[["da4005ed36f072d2"]]},{"id":"3c670cdc32ad3980","type":"debug","z":"4895ea10b4ee9ead","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":600,"wires":[]},{"id":"da4005ed36f072d2","type":"debug","z":"4895ea10b4ee9ead","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":520,"wires":[]},{"id":"2474195798573f55","type":"comment","z":"4895ea10b4ee9ead","name":"api requests","info":"","x":290,"y":380,"wires":[]},{"id":"a5e710f2510b7c79","type":"inject","z":"4895ea10b4ee9ead","name":"2nd request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"center_id\":1111,\"name\":\"AAAAA\",\"sessions\":[{\"session_id\":\"A111\",\"date\":\"19-07-2021\"},{\"session_id\":\"B111\",\"date\":\"20-07-2021\"},{\"session_id\":\"C111\",\"date\":\"21-07-2021\"}]},{\"center_id\":2222,\"name\":\"BBBB\",\"sessions\":[{\"session_id\":\"A222\",\"date\":\"19-07-2021\"},{\"session_id\":\"B222\",\"date\":\"20-07-2021\"}]},{\"center_id\":3333,\"name\":\"CCCC\",\"sessions\":[{\"session_id\":\"C111\",\"date\":\"19-07-2021\"}]},{\"center_id\":4444,\"name\":\"DDDD\",\"sessions\":[{\"session_id\":\"D111\",\"date\":\"19-07-2021\"},{\"session_id\":\"D222\",\"date\":\"19-07-2021\"}]}]","payloadType":"json","x":290,"y":560,"wires":[["7a098efeb56cc28b","3c670cdc32ad3980"]]},{"id":"0b9ca46e72b2d304","type":"inject","z":"4895ea10b4ee9ead","name":"3rd request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"center_id\":1111,\"name\":\"AAAAA\",\"sessions\":[{\"session_id\":\"A111\",\"date\":\"19-07-2021\"},{\"session_id\":\"B111\",\"date\":\"20-07-2021\"},{\"session_id\":\"C111\",\"date\":\"21-07-2021\"}]},{\"center_id\":2222,\"name\":\"BBBB\",\"sessions\":[{\"session_id\":\"A222\",\"date\":\"19-07-2021\"},{\"session_id\":\"B222\",\"date\":\"20-07-2021\"}]},{\"center_id\":3333,\"name\":\"CCCC\",\"sessions\":[{\"session_id\":\"C111\",\"date\":\"19-07-2021\"}]},{\"center_id\":4444,\"name\":\"DDDD\",\"sessions\":[{\"session_id\":\"D111\",\"date\":\"19-07-2021\"},{\"session_id\":\"D222\",\"date\":\"19-07-2021\"}]},{\"center_id\":5555,\"name\":\"EEEE\",\"sessions\":[{\"session_id\":\"E111\",\"date\":\"19-07-2021\"},{\"session_id\":\"E222\",\"date\":\"19-07-2021\"},{\"session_id\":\"E333\",\"date\":\"19-07-2021\"}]}]","payloadType":"json","x":290,"y":660,"wires":[["7a098efeb56cc28b","3c670cdc32ad3980"]]}]