Need help with handling quotes in JSON object for HTTP request in n8n

Hello everyone,

I am currently working on an n8n workflow where I am using the HTTP Request node to send requests. The request body is a JSON object which includes a ‘queries’ property.

The value of ‘queries’ is a string that includes search phrases surrounded by double quotes (") as part of the search criteria. For example:

swiftCopy code

"queries": "site:stackoverflow.com/users/ \"Artificial Intelligence\" OR \"AI\" OR \"Machine Learning\" OR \"ML\" OR \"Deep Learning\" OR \"Neural Networks\" OR \"Tensorflow\" OR \"Pytorch\" \"Munich\"",

I’ve noticed that when my ‘queries’ string includes these double quotes, I get an error in the n8n HTTP Request node that says “JSON parameter need to be a valid JSON”. But when I use a simpler string without double quotes, the request goes through fine.

I’ve tried various ways to escape the quotes, including using backslashes (") and replacing the double quotes with single quotes ('), but I still get the same error. I’ve also tried using different methods to construct the ‘queries’ string dynamically in a Function node before the HTTP Request node, but this hasn’t solved the issue either.

Has anyone faced a similar issue, or does anyone have suggestions on how to handle this? Any help would be greatly appreciated.

Thank you!

Please share your workflow




## Share the output returned by the last node


{
  "meta": {
    "instanceId": "240223e06656e67cee425800d602013033815f933bef971218cf5a1dde8ef93f"
  },
  "nodes": [
    {
      "parameters": {
        "jsCode": "\nfor (const item of $input.all()) {\n  item.json.myNewField = 1;\n  item.json['query'] = item.json['query'].replace(/\"/g, \"'\");\n  item.json['nb_profiles'] = parseInt(item.json['nb_profiles']);\n  item.json['country'] = item.json['country'].toLowerCase();\n  item.json['language'] = item.json['language'].toLowerCase();\n\n}\n\nreturn $input.all();\n"
      },
      "id": "5cb0b4d8-cf33-4941-9169-577f8d9c8ce8",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        940,
        440
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.url }}",
              "operation": "contains",
              "value2": "stackoverflow.com"
            }
          ]
        }
      },
      "id": "84618a3e-ce6b-47f2-9d04-c8136ef1382b",
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1360,
        440
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "documentId": {
          "__rl": true,
          "value": "1G5MBU9kyvShOUVbdmFjZZKZtwAxN-x43Gh_hqwGu9vo",
          "mode": "list",
          "cachedResultName": "Candidates_data",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G5MBU9kyvShOUVbdmFjZZKZtwAxN-x43Gh_hqwGu9vo/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "entry",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G5MBU9kyvShOUVbdmFjZZKZtwAxN-x43Gh_hqwGu9vo/edit#gid=0"
        },
        "options": {}
      },
      "id": "4c34b2eb-1ab4-4f21-9a70-0e349226a127",
      "name": "Candidates Data",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "typeVersion": 1,
      "position": [
        720,
        440
      ],
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "id": "20",
          "name": "Google 3"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.apify.com/v2/acts/apify~google-search-scraper/run-sync-get-dataset-items?",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer REDACTED"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={       \n\"queries\": \"{{ $json[\"query\"] }}\",\n\"countryCode\": \"{{ $json[\"country\"] }}\",    \n\"includeUnfilteredResults\": false,    \n\"languageCode\": \"{{ $json[\"language\"] }}\",    \n\"maxPagesPerQuery\": 1,    \n\"mobileResults\": false,    \n\"resultsPerPage\": {{ $json[\"nb_profiles\"] }},    \n\"saveHtml\": false,    \n\"saveHtmlToKeyValueStore\": false,    \n\"maxConcurrency\": 10 \t  \n}",
        "options": {}
      },
      "id": "6cdaa930-e5da-4afa-9ba4-8f66db5a2b0b",
      "name": "Google Search Results",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1160,
        440
      ]
    }
  ],
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Google Search Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Candidates Data": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Search Results": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @wilsoncelyCUC :wave: Welcome to the community! :tada:

First and foremost - I wanted to let you know that you accidentally shared an API token in your workflow details code block, which I’ve redacted for you. But you might want to rotate your API keys :see_no_evil:

I have a feeling you’re going to want to return a single JSON object, like this:

{{ {       
"queries": $json["query"],
"countryCode": $json["country"],
"includeUnfilteredResults": false,
"languageCode": $json["language"],
"maxPagesPerQuery": 1,
"mobileResults": false,
"resultsPerPage": $json["nb_profiles"],
"saveHtml": false,
"saveHtmlToKeyValueStore": false,
"maxConcurrency": 10
} }}

They way you’re currently returning the object is a string within a string - when you wrap the expressions in quotes like { "foo": "{{$json.query}}" } it will return only text, but if you return {{ { "foo": $json.query } }} it will be the actual JSON object, which is what you’re going to want :slight_smile: When you start an expression with {{ and end it with }} n8n considers the whole expression to be JavaScript, so quotes inside a field like $json.query would be properly escaped.

Something like this will work, and you know you’re on the happy path if you see that little "Object: " bit in front of the preview of your expression:

Let me know if that helps you out!

4 Likes

Thanks! Im so rookie :slight_smile: Ive rotated my key

As per the issue, Indeed, it worked fine with your solution… and your explanation was straight-forward ,so big thanks!

Have a great week!

2 Likes

You have a wonderful week, too - I’m so glad that helped! :smiley:

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