Shorten a string (input) from the previous node to 200 characters

I am dying here… I can’t figure out how to shorten the input on a node. This is a json element that contains the content of an email, and it at times is full of garbage due to people’s crappy (usually Outlook) email clients. I would like to limit the returned value to only 200 characters.

I have tried slices, substrings, and I am just stuck hitting a wall. Below is the mock data of the element I have plucked out and I want to transform. Please, can someone help me figure this out, it surely must be simple, because complex stuff I usually figure out… it’s the simple stuff that stumps me. :roll_eyes:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Jesse_Perry,

Maybe I am missing something but a simple slice should work here. I quickly updated your code node(love the name btw) to showcase it.
Hope this helps, let me know if there is anything more

1 Like

Alternatively, you can do this in an expression:

Or, if you want to break on a word boundary only and add some ellipsis to indicate the string was truncated:

TL;DR: expressions are awesome :wink:

*edit: I just noticed a typo in the expression - it refers to $json[‘content’] at one point instead of $json[‘node’]. Sorry!

4 Likes

I tried the expression… and in the preview it looks good… but in practice it outputs an empty array. Perhaps I can’t use that in an Item Lists node?

I even tried just putting the expression in the last step, where it creates the ending payload (google chat object). But it appears to just ignore the slice entirely.

The secret sauce seems to be selecting the element with [0], so the working expression is {{ $json.note[0].slice(0,200) }} in the last stage. This works.

The field ‘input field name’ should only contain the actual name of a field, not a value like this.

2 Likes

Ahh… and that’s why it wouldn’t work. I wish it failed the evaluation rather than give me pretty green text to make me thing it was good. Ah well… maybe someone will automate that :smiley:

Thank you SO MUCH!

1 Like

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