# \[SOLVED\] Msg.payload : objects to string in a sentence (need help !)

**URL:** <https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167>\
**Category:** General\
**Created:** [16 March 2020 11:05 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167 "2020-03-16T11:05:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [16 March 2020 11:05 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/1 "2020-03-16T11:05:34Z")

</div>

Hello you who reads me. 😉  
It's been a year since I started on Node-red, I'm learning little by little, and doing great things. But there are still points that draw my hair, it is the manipulation of objects, tables etc.  
Here is an example: a study counts the number of first names in a school class **(we do not know the total number of students):**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58201e44f56b8dca2558ffb489aeb9939d4ce2d4.png)

I would like to return an msg. as a String:  
`"There are 3 johns, 1 paul, 2 juliette etc ..."`

```auto
msg.classes = {john:3,paul:1,juliette:2}
return msg

```

I think there are object to array conversions. And that's where everything gets muddled. 🤪  
Any help is appreciable  
Chris

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 March 2020 11:28 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/2 "2020-03-16T11:28:04Z")

</div>

UPDARE: Oops, I should hav read it better - I thought you wanted the total students.

I love jsonata - add a change node using this jsonata expression `$sum(classes.*)`

````auto
[{"id":"dab3ac00.d82d","type":"change","z":"10b72cef.116283","name":"","rules":[{"t":"set","p":"total","pt":"msg","to":"$sum(classes.*)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":260,"wires":[["11a5247e.32987c"]]}]```

the answer will be in msg.total
````

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 March 2020 11:29 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/3 "2020-03-16T11:29:58Z")

</div>

Hi @SuperNinja

you caught me at a good time... here's a Function node that will generate the sentence you want. I've even handled pluralisation of the names for you because its a Monday morning and I've got plenty of real work I should be doing.

```auto
// Get the array of names
var names = Object.keys(msg.classes);

var firstCount;

// For each name, map it to "n name"
var parts = names.map((n,i) => {
    var count = msg.classes[n];
    if (i === 0) {
        //Remember the first count to get the "is/are" right later
        firstCount = count;
    }
    // Return "n name" and get the pluralisation right
    return count+" "+n+(count>1?"s":"")
})

// If there is more than one name, pop off the last one for later
var lastName;
if (parts.length > 1) {
    lastName = parts.pop();
}

// Build up the response getting "is/are" right for the first count and joining
// the array of names with a comma
msg.payload = "There "+(firstCount === 1 ? "is":"are")+" "+parts.join(", ")

// If there was a last name, add that on the end with an 'and', not a comma
if (lastName) {
    msg.payload += " and "+lastName;
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [16 March 2020 11:49 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/4 "2020-03-16T11:49:20Z")

</div>

I note it anyway, thanks

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [16 March 2020 11:50 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/5 "2020-03-16T11:50:02Z")

</div>

pretty! thank you !  
Now I have time to understand how it works 👍

---

<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:** [30 March 2020 11:50 UTC](https://discourse.nodered.org/t/solved-msg-payload-objects-to-string-in-a-sentence-need-help/23167/6 "2020-03-30T11:50:07Z")

</div>

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