# Help with JSONata

**URL:** <https://discourse.nodered.org/t/help-with-jsonata/71757>\
**Category:** General\
**Created:** [6 December 2022 09:46 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757 "2022-12-06T09:46:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 09:46 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/1 "2022-12-06T09:46:29Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05b9433d0f0f792956fc7f7b18dd49e978514f13.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 December 2022 10:13 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/2 "2022-12-06T10:13:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 10:19 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/3 "2022-12-06T10:19:31Z")

</div>

Hi @Colin ,

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 December 2022 10:33 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/4 "2022-12-06T10:33:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 December 2022 10:46 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/5 "2022-12-06T10:46:06Z")

</div>

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

e.g.

```auto
const names = msg.payload.map(e => e.name)
msg.payload = names
return msg

```

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 10:50 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/6 "2022-12-06T10:50:17Z")

</div>

ohh I forgotten about that my bad!

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 10:52 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/7 "2022-12-06T10:52:51Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14319d75ca83108b7f2252d5baf3c89fa053e74b.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 December 2022 10:54 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/8 "2022-12-06T10:54:23Z")

</div>

Exactly what do you want out?

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 10:58 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/9 "2022-12-06T10:58:19Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05b9433d0f0f792956fc7f7b18dd49e978514f13.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 December 2022 11:03 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/10 "2022-12-06T11:03:54Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [6 December 2022 11:05 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/11 "2022-12-06T11:05:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 December 2022 12:26 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/12 "2022-12-06T12:26:48Z")

</div>

You first ask "i only want the names for both routers" now you want text from multiple properties - all in one line!

> [@Kaij3](#):
>
> Type: "Router" Total\_Quantity\_left: "0", Type: "Bosch Router" Quantity: "0"

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)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5abb2adefb58e3a8f228ce7b04395f215dab3004.png)

Function code (ES6 way)

```auto
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)

```auto
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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 December 2022 12:36 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/13 "2022-12-06T12:36:14Z")

</div>

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

```auto
$$.payload.name

```

will return an array of names

And to output as post 9 example

```auto
$join(
   $$.payload.(
       'Type: "' & $.Type & '" Total_Quantity_left: "' & $.Total_Quantity_left & '"'
   ),
   ", "
)

```

---

<div class="post-metadata">

**Author:** ![Kaij3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kaij3/32/60268_2.png) [@Kaij3](https://discourse.nodered.org/u/Kaij3)\
**Post date:** [8 December 2022 17:44 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/14 "2022-12-08T17:44:39Z")

</div>

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! 😓

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 December 2022 17:44 UTC](https://discourse.nodered.org/t/help-with-jsonata/71757/15 "2022-12-22T17:44:46Z")

</div>

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