I am facing issues where I am trying to query specific data from an array, example, if i only want the names for both routers , how do i do it? As I searched up, using jsonata would be easier but i am still new to using JSONata.
It would be really great if you could guide me on this!
What do you mean by didn't display anything? Feed the function node into a debug node to see what it sends. The line I posted puts the new array into a variable, did you put that in msg.payload?
That doesn't seem to be much related to the original question. For that I think you will need a forEach loop that builds up the string. Or Array.reduce() to do the same thing,
You first ask "i only want the names for both routers" now you want text from multiple properties - all in one line!
Please be concise with your questions & avoid changing your mind as we all contribute our free time helping out.
Moving on...
You can achieve a single line of text by looping through the array and generating a new string consisting of the parts you want. Then join the resulting strings with a ", "
Here is the result (of some dummy data) in the format you request (the function code will follow)
Alternative Function code (long hand for beginners)
const inputData = msg.payload
const formattedData = [] // make a new empty array
// loop the input array and make a formatted string
// then add that string to the new array
for (let index = 0; index < inputData.length; index++) {
const entry = inputData[index];
const entryAsString = 'Type: "' + entry.Type + '" Total_Quantity_left: "' + entry.Total_Quantity_left + '"'
formattedData.push(entryAsString)
}
const singleString = formattedData.join(', ')
msg.payload = singleString //set the msg's payload to the result
return msg; // return the msg to next node