Formatting in HTTP Request Node - Remove all special characters with multiple variables in one JSON value

Dear n8n Community,

I am trying to write a HTTP Request node, in this case to OpenAI and have quite a big promt in there.
My problem is that the promt contains multi-line text with many special characters and throws an error that the formatting is not correct.

Promt looks like this:

  "prompt": "Write  a summary for the following Text on {{$json["data"]["title"]["en"]}} with maximum 70 characters: {{$json["data"]["description"]["en"]}}",
  "temperature": 0.0,
  "max_tokens": 25,
  "top_p": 1.0,
  "frequency_penalty": 1.4,
  "presence_penalty": 0.0

This is a example title:

n8n Open Source Workflow Automation

This is a example description text with multiple special characters (",';(),• etc.) lists etc. which all needed to be fit in the promt, with correct JSON formatting:

n8n is an open-source Node.js-based workflow automation tool that enables users to connect various data sources and services to create automations. Some of n8n's best features include its ease of use, scalability, and extensibility. n8n is also highly customizable, allowing users to create workflows that fit their specific needs. 

n8n's “automations” can be triggered by events, such as a new record being added to a database, or they can be run on a schedule. n8n also supports conditional triggers, so that an automation can be run only when certain conditions are met. N8n automations can perform a wide range of actions, including creating and updating records, sending emails/messages, making “HTTP requests”, and running shell commands. N8n's (modular) design makes it easy to add new actions as needed. 

N8n is constantly being improved and updated by its growing community of users and developers. New features are always being added, and the software is continually optimized for performance. 

    • n8n is a free and open-source 
    • n8n provides a visual interface
    • n8n supports a wide range of nodes 
    • n8n can be easily extended 
    • n8n is cross-platform and runs on Windows, Linux, and macOS. 

I saw many posts about the formatting on single entries, like removing one quotation at the beginning or the end, but they are not really working on this kind of promt.

Hopefully someone knows how to solve this problem. Have a great week!

Hi @prononext,

To strip out unwanted characters, regular expressions would be your best bet. Here is an example workflow:

This example uses Javascript’s replace method and a regular expression of /[^a-z0-9\s]/gi (finding all characters except a to z, 0 to 9 and spaces).

Hope this helps!