Replacing certains strings of a string field using fields from the JSON array

Describe the issue/error/question

Hi, Trying to create a dynamic string field by replacing the tagged strings and replace with field data from the JSON Array.

Basically I receive a list of products, but then I would like to allow users to change the description to a new text. Before sending data into n8n, they can choose how to write the new description by writing a new field as “new_description” like example below

> This product {title} is {description} priced at {price}

so in this a new field called “new_description” should be added to the array, and rewritten replacing in this case title, description and price, being updated with the data of the respective field.
The title, description, price cannot be static since each product owner will be adding the format of the new description as they see fit.

I tried using an example that I have found in another topic, but not working so far, I mixed the flow as I was trying 2 different ways, but just leaving here to get an idea. I initially started with a".replace().replace()" but must be an easier and faster way to perform this. Thank you very much.

Please share the workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]:

Hi! :wave: Try this in your Set node expression (the node named Set6 in your example)

{{ "This product " + $json["title"] + " is " + $json["description"] +" priced at " + $json["price"] }}

This takes care of adding a new description. If you only want the new description, enable Keep Only Set.

Hi @deborah,
But the text is static in this case, apologies, maybe was not clear, but all written text and fields need to dynamic.
So need to find a way that when I receive the text with the tags in a different ways, I can map and get the data from the JSON array into the new description field for each product.
What happens is that one user can write one way, the other user another way (Sort like a vendor describing their own product):

User 1:
This product {title} is {description} priced at {price}

User 2:
*This mobile {brand}, model {title} has stock {stock } *

And so on,

So this data wrapped in the {} should be coming from the json itself but can always be different.

Another similar example that I found from another topic, but not with dynamic text or fields

Thank you :slight_smile:

How are your users going to input their text templates? Will they be able to edit the workflow, or do you need a way for them to pass in their text (and if so, from where?)

And: what output do you need? Am wondering if our HTML node might be relevant: HTML - n8n Documentation
It allows you to generate HTML templates.

Hi Deborah,
So the vendor in the front end has a CSV that uploads in the frontend with that it enriches my database as the example in flow with the dummy data of the products. Each vendor has 1 way of writing the new description with a specific string format and fields to be updated, example of a vendor X and vendor Y

Vendor X

new description
The phone {title} priced at {price} has stock {stock}

Vendor Y

new description
Check {Brand} - {title} - {description} priced at {price} has stock {stock}

When a vendor submits a CSV, then goes into n8n and I pull the data to n8n the JSON response of the product DB would be like:

Vendor X

Vendor Y

So the new description field should be updated inside by replacing the {} words with the proper info from the respective columns.

Example of Vendor X new description:

So the original answer cannot be used, because it is always using a specific structure, should be dynamic as the last workflow I shared. Thank you :slight_smile:

Could you send a copy of the dummy JSON? I’m currently getting “too many requests”.

And can I check it contains some example new descriptions?

It’s basically the same as this one, I just separated in 3 as if it was 3 different vendors but in the end they all use the same flow and not 3. No worries, I appreciate the time already given.

If each vendor always uses the same pattern for their description, then this might work:

  1. Input from your database
  2. A Switch node to go down different paths depending on which vendor
  3. Then the Set node with the expression

If each vendor won’t always use the same description, then we can still do it. But it would be useful to have the exact input you expect. At the moment, your dummy JSON doesn’t seem to include the new description template lines (but looking at your spreadsheet images, this should come along with the rest of the data). Your dummy data also doesn’t indicate which vendor its from.

Althought I “register” their format in a DB, they can always change. Also we can 10 vendors today, but tomorrow 50/100, that would not be ideal due to always adding switchs manually. Ideally during the n8n process, they should be mapped dynamically according to that “new_description_format” field.

The JSON does not indicate the vendor, because it should be vendor agnostic, for a record, it should look into the new_description for that record, then pickup the necessary data from the fields (E.g. “title”, “brand”, and so on) and then update the new_description field accordingly :slight_smile:

But idea would be to avoid so many replaces and be more dynamic with a sort of mapping/while/loop style.

An example with a lot of replaces :smiley: .

You’re almost there with the chain of replaces.

I created a simplified version with some simplified example data, but hopefully it shows the concepts.

  1. I use the Manual Trigger node for testing, and pin some dummy data as the output.

  2. Then in the Set node, I use this expression:

    {{ $json["customDescription"].replace("{title}", $json.title).replace("{price}", $json.price).replace("{stock}", $json.stock) }}
    

As you can see, it adds a new description to each item, with the values swapped out. It doesn’t break even if they don’t use all the values. You can add more values

2 Likes

Thank you so much @deborah, you are a life savior :star_struck:. It seems to be working perfectly :slight_smile: .

2 Likes

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