Response Data Parsing Into A New Post Request Issue \n

My goal is to send text to Open AI for embedding models.
I have a HTTP response object thats returned with a string including new line characters

//this shows in the input preview of current HTTP request node
[{
   thestring: "Lorem ipsum dolor sit amet\nconsectetur adipiscing elit. Nunc\n
   luctus risus erat, sit amet varius elit finibus\n..." 
},{
     thestring: "Lorem ipsum dolor sit amet\nconsectetur adipiscing elit. Nunc\n
   luctus risus erat, sit amet varius elit finibus\n..." 
}]

The expression result shows to have errors because the “\n” are removed
-https://api.openai.com/v1/embeddings

// the error in fixed view
Bad control character in string literal in JSON at position 37 (line 2 column 36)

//Inside the json configuration editor

{
     "input": "{{ $json[$index]['thestring'] }}",
    "model": "text-embedding-3-small",
    "encoding_format": "float"
}

Expression Result output
-------------
// notice the \n are removed. 
{
     "input": ""Lorem ipsum dolor sit amet 
     consectetur adipiscing elit. Nunc
   luctus risus erat, sit amet varius elit finibus
   ..."",
    "model": "text-embedding-3-small",
    "encoding_format": "float"
}

Is theres a way to keep the \n or replace the \n with " " or any other suggestion would be great

Information on your n8n setup

  • n8n version: 1.52.2
  • n8n EXECUTIONS_PROCESS setting: default
  • Running n8n via n8n cloud
  • Operating system: windows

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 @quickee, thanks for posting :sunflower:
It would be helpful to see how the data is passed between the nodes, could you maybe share your workflow JSON instead of the example data you’ve posted above? Also, what do you mean by ‘json configuration editor’ here?
You can also try using replace('\n', '\\n') to include double backslashes (\\) instead of single backslashes (\) for the newline characters. Would that help?

1 Like

Hi there @aya

I can post my data shortly.

I did attempt string.replace(“\n”, " "), that did not work. But I will give double backslash a try.

re: “json configuration editor (Expression)” - there are three sections when in node editor. Left (Input) - Middle (Editor) - Right (Output). The middle section has editors for mapping “fixed, expression” etc.

If you switch to “fixed” display, the JSON shows to have that error in my original post. Which is the “Expression Result output”

Thanks

If you want to use input from previous nodes within a text, you’ll have to use Expression mode instead of Fixed.
Which nodes are you using? A screenshot or sharing your workflow json by selecting the relevant nodes and pasting in the json between ``` here would be great to understand what’s going!

In the example you provided it looks like you have double quotes around the input key. Could that have something to do with it?

// notice the "". 
{
     "input": ""Lorem ipsum dolor sit amet 
     consectetur adipiscing elit. Nunc
   luctus risus erat, sit amet varius elit finibus
   ..."",}

David’s right, since {{ $json[$index][‘thestring’] }} is already a string, removing the double quotes around it in the input field should help! Try it out and let us know how it goes.

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