Quotation marks in raw json suddenly in new line

Hey,

I have a workflow making multiple HTTP requests and sending information in raw json.

Everything always worked, but all of a sudden quotes in some nodes are pushed to the next line and the JSON is invalid.

Example - Input (expression):

{
"first_name": "{{$node["SplitInBatches2"].json["person3"].split(" ").slice(0, -1).join(" ")}}",
"last_name" : "{{$node["SplitInBatches2"].json["person3"].split(" ").slice(-1)[0]}}",
"organization_ids" : [{{$node["Set3"].json["company_id"]}}]
}

Result:

{
"first_name": " Max",
"last_name" : "Mustermann
",
"organization_ids" : [284251840]
}

As you can see there is a " in wrong place…

Why is that and how can I fix it?

Kind regards
Silas

Hi @Silas_Hundhausen, is there by any chance a line break in your input data? n8n wouldn’t show linebreaks in the node details view (to allow viewing more content in a limited space), but it also wouldn’t add line breaks.

As a simple counter measure you could try removing linebreaks from your strings by appending something like .replace(/\n/g,'') to your expression (this would replace all linebreaks represented by \n with an empty string).

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "example_without_linebreak",
              "value": "={{$json[\"example\"].replace(/\\n/g,'')}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "example_without_linebreak",
              "value": "={{$json[\"example\"].replace(/\\n/g,'')}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.example = `Mustermann\n`;\nconsole.log(item);\nreturn item;"
      },
      "name": "Set Example",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey,
thank you for the reply. However, there is no line break – I have also tried to write everything in 1 row but there still is a line break.

.replace(/\n/g,’’) ← Does not work

Oh, the line break would be in your data, not in your n8n expression from the looks of it. So somewhere in the person2 field of your SplitInBatches node.

Maybe you can provide a dummy workflow including some mock data allowing me to reproduce the problem?

Hey,
there are no line breaks in the input field. To make sure, I just replaced all linebreaks in my Google-Sheet-Input: Remove line break within cell google spreadsheet - Stack Overflow

Screenshot 2021-12-17 at 14.38.14

Well, the line break has to come from somewhere :wink:

Anyway, the .replace() syntax in your screenshot would be wrong as it would search for literal occurrences of /\n and replace these which isn’t what you want. Can you try writing your second expression like below so it uses a valid regular expression matching your line break?

{{$node["SplitInBatches2"].json["person2"].split(" ").slice(-1)[0].replace(/\n/g, "")}}
{
"first_name": "{{$node["SplitInBatches2"].json["person2"].split(" ").slice(0, -1).join(" ")}}",
"last_name" : "{{$node["SplitInBatches2"].json["person2].split(" ").slice(-1)[0].replace(/\n/g, "")}}",
"organization_ids" : [{{$node["Set3"].json["company_id"]}}]
}

Anything is wrong here, don’t know what.

Screenshot 2021-12-17 at 14.44.14

Yes, it’s lacking a double quote after person2. You’d simply need to insert .replace(/\n/g, "") at the end of your existing expression, just before the closing curly braces }}.

Still not working…

So your last name might include another newline character in this case. It would really really help if you could share an example workflow including some mock data using which the problem can be reproduced.

Edit: Something like this:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    value_with_linebreak: `Cabildo\n`\n  }\n}];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "foo",
              "value": "={\n\"last_name\": \"{{$json[\"value_with_linebreak\"].replace(/\\n/g, '')}}\"\n}"
            }
          ]
        },
        "options": {}
      },
      "name": "Remove Linebreak",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Remove Linebreak",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Well, I just tried to reproduce the error in the style of the set you sent.

However, I can’t reproduce the error.

That’s probably because .replace() is working with the line break in my example. I suspect in your case there is some other special character in the string you’re trying to modify.

You could also try a broader regular expression, something like .replace(/\r?\n|\r/g, '') found here:

Thank you very much for the help.

I think i found the error. There was an empty space before the name like " Max". Once I removed the space, it worked again. I will do some more test cases and will get back to you if there is an another error.

Thank you!

1 Like

Awesome, glad to hear you figured it out!