Find object in an array, based on other input string

Greetings together,

I've got the following situation:
I'm trying to match a string with a corresponding object in an array and return this object as payload.

e.g. the string in msg.topic is "-5421010250119599039-2006151152-6" and the array is

msg.payload:

[
{"$":
{"id":"-7947913692768115784-2006151118-32","eva":"8004404"},
"ar":[{"$":{"ct":"2006151248","l":"2"}}],
"dp":[{"$":{"ct":"2006151249","l":"2"}}]},

{"$":{"id":"-5421010250119599039-2006151152-6","eva":"8004404"},
"ar":[{"$":{"ct":"2006151211","l":"2"}}],
"dp":[{"$":{"ct":"2006151212","l":"2"}}]},

{"$":{"id":"-2031174509165804068-2006151018-32","eva":"8004404"},
"ar":[{"$":{"ct":"2006151148","l":"2"}}],
"dp":[{"$":{"ct":"2006151149","l":"2"}}]},
]

I want to find the object in the string, where msg.topic == msg.payload[i].$.id and return msg.payload[i].$.ar or msg.payload[i].$.dp

I tought about using a function like

var id = msg.topic;
var payload = msg.payload;
var i;
var a;

for (i = 0; i < msg.payload.length; i++)
a = msg.payload(i).$.id;
if (a == id){

return msg;
}

But it did not work.

Any ideas on this?

Thanks in advance

You should be able to use Array.find to do that. It goes through the array, calling a function for each one, and returns the first matching element.

You can also use a JSONata query in the change node to extract that information.

Here below the query for your problem.

https://try.jsonata.org/eBEhhbcqs

payload["$".id = $$.topic ]{
     "ar" : ar,
     "dp" : dp
}

Note that you need to put $ between quotes in the payload filter as $ without quotes is a variable referring to the current context in a jsonata expression.

Thanks both.

Since msg..topic and msg.payload are from 2 different sources, I guess i must join them anyhow first?

image

[{"id":"34ef0afe.9a44b6","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"27433d2.7800bc2","type":"http request","z":"34ef0afe.9a44b6","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"https://api.deutschebahn.com/timetables/v1/fchg/8004404","tls":"","persist":false,"proxy":"","authType":"bearer","x":430,"y":100,"wires":[["33d6eb77.12a564"]]},{"id":"9d9966c7.d4f358","type":"inject","z":"34ef0afe.9a44b6","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":200,"wires":[["87feadb9.5b8dc","27433d2.7800bc2"]]},{"id":"b1b0b903.57d438","type":"http request","z":"34ef0afe.9a44b6","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"https://api.deutschebahn.com/timetables/v1/plan/8004404/{{{payload}}}","tls":"","persist":false,"proxy":"","authType":"bearer","x":410,"y":260,"wires":[["56dd9042.7d7d"]]},{"id":"87feadb9.5b8dc","type":"time-comp","z":"34ef0afe.9a44b6","outputs":1,"name":"","positionConfig":"aba1f65b.eda0b8","input":"","inputType":"date","inputFormat":"0","inputOffset":0,"inputOffsetType":"none","inputOffsetMultiplier":60000,"rules":[],"checkall":"true","result1":"","result1Type":"msgPayload","result1Value":"","result1ValueType":"input","result1Format":"yyMMdd/HH","result1Offset":0,"result1OffsetType":"none","result1OffsetMultiplier":60000,"x":200,"y":260,"wires":[["b1b0b903.57d438"]]},{"id":"56dd9042.7d7d","type":"html","z":"34ef0afe.9a44b6","name":"","property":"payload","outproperty":"payload","tag":"s","ret":"attr","as":"single","x":630,"y":220,"wires":[["236b8ce3.fc1f54"]]},{"id":"236b8ce3.fc1f54","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload[0].id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":180,"wires":[[]]},{"id":"33d6eb77.12a564","type":"xml","z":"34ef0afe.9a44b6","name":"","property":"payload","attr":"","chr":"","x":630,"y":100,"wires":[["73948ba8.543a14"]]},{"id":"73948ba8.543a14","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.timetable.s","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":100,"wires":[[]]},{"id":"ad94e4f1.6168f8","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[\"$\".id = $$.topic ]{      \"ar\" : ar,      \"dp\" : dp }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":160,"wires":[[]]},{"id":"626820e1.c325d","type":"function","z":"34ef0afe.9a44b6","name":"find","func":"var id = msg.topic;\nvar payload = [msg.payload[0], msg.payload[1], msg.payload[2], msg.payload[3]];\n\nfunction findTrain(payload){\n        return payload == id;\n}\n\nfunction myFunction(){\n        document.getElementById(\"$.id\").innerHTML = payload.find(findTrain);\n}\n\nmsg = payload;\nreturn msg;\n","outputs":1,"noerr":0,"x":1070,"y":120,"wires":[[]]},{"id":"aba1f65b.eda0b8","type":"position-config","z":"","name":"Niederroth","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"}]

You have launched the http requests in parallel.
I think it is better to launch the http requests in series then you don't need to join.

Thank you both,

I have used the solution of @janvda.

The whole thing looks not quite complex :slight_smile:

image

1 Like

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