Search in a Json

Hi

I need to make a search in a Json to find a number, and then return an other number, i did try to make a little flow to explane my quest.

my json is converted from a xml and imported from a file, but this is not in this demo flow, so it is not intended to be in a inject node

I uploaded the flow because it is to big to insert here.

flows.json (171.8 KB)

Can someone please help me in the right direction ?

Hello Zamzon,

msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")
return msg;

this will return the first matching object from the array of DeviceDetails where its DeviceInfo.box_number equals with string 46 (note that your values are stored as strings so check for strings)

afterwords i didnt understand the criteria to return the logical_area since logical_area is in the array LoadControlChannelList

Thanks,

It should look under LoadControlChannelList under the right Deviceinfo for "channel" (2) and then return Logical_area for the right LoadControlChannelList

I updated the function.

var box_number = 46;
var channel = 2; // LoadControlChannelList

//Search in DeviceDetails  0-26 for box_number 46 

//It's found here:
//payload.DeviceDetails[3].DeviceInfo.box_number

//And then print the logical_area for the right DeviceDetails:

//msg.payload = msg.payload.DeviceDetails[3].LoadControlChannelList[2].logical_area
msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")

return msg;

ok .. this makes more sense .. so you need from channel (2)

msg.payload = msg.payload.DeviceDetails.find(el => el.DeviceInfo.box_number === "46")
msg.payload = msg.payload.LoadControlChannelList[2].logical_area   // channel(2)

return msg;

Thanks,
it works perfect :slight_smile:

Hi

I'm trying to insert the new code into the rest of my code, then I can't get box_number from my float

this dosn't work and return 46

boxnr = flow.get("boxnr");  

this works perfect and return "46"

boxnr = "46"; 
boxnr = flow.get("boxnr"); 
var channel = 2;

msg.payload = msg.payload.DeviceExport.DeviceDetails.find(el => el.DeviceInfo[0].$.box_number === boxnr)
msg.payload = msg.payload.LoadControlChannelList[0].LoadControlChannel[channel].$.logical_area

I'm receiveing 46 from my float.get, but i can only get it to work if I insert "" so it looks line "46"

The === means exact match. I.e. 46 is not === equal to "46"

You could use parseInt around the values or change to == or coerce the boxnr to a string e.g. var boxnr = "" + flow.get("boxnr")

Thanks again, it works perfectly :partying_face:

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