Help with JSONata

Hello everyone!

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!

image

Personally I prefer to use a function node. Something like
const names = msg.payload.map(e => e.name)

Hi @Colin ,

Thanks for the suggestion , but when i enter the code into the function node, it didn't display anything.

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?

Did you leave return msg at the bottom of the function?
Did you set the msg.payload?

e.g.

const names = msg.payload.map(e => e.name)
msg.payload = names
return msg
1 Like

ohh I forgotten about that my bad!

It works! I am able to get the data, but is there a way I can combine these two names into one sentence?

image

Exactly what do you want out?

What i would like out from the output is,

For example Type: "Router" Total_Quantity_left: "0", Type: "Bosch Router" Quantity: "0" as the output in a single line.

But not sure if it could be done.
image

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,

is there an example? where i can achieve this function.

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)

Function code (ES6 way)

msg.payload = msg.payload.map(e => {
    return `Type: "${e.Type}"  Total_Quantity_left: "${e.Total_Quantity_left}"`
}).join(', ')
return msg;

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

To answer the OG question for future readers that are looking for a JSONata answer

$$.payload.name

will return an array of names

And to output as post 9 example

$join(
   $$.payload.(
       'Type: "' & $.Type & '"  Total_Quantity_left: "' & $.Total_Quantity_left & '"'
   ),
   ", "
)
1 Like

I apologize for not correctly formatting my questions.

But Thank you so much for your guidance, I will try it out!

Once again Thank you very much! :sweat:

1 Like

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