Query params to JSON

Describe the issue/error/question

I need to transform query string into json,I’ve been trying to use URLSearchParams via url module (imported) but I’m getting the error below.

What is the error message (if any)?

TypeError: URLSearchParams is not a constructor
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/FunctionItem:6:14

Please share the workflow

Share the output returned by the last node

TypeError: URLSearchParams is not a constructor
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/FunctionItem:6:14
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/FunctionItem:8:2
    at BaseHandler.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/bridge.js:479:11)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/nodevm.js:425:23)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/FunctionItem/FunctionItem.node.js:107:41)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:592:51)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:455:64
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Expected output: “foo”,
all params as json object is preferred (could be done once I have all keys to iterate on)

Information on your n8n setup

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

Hey @ToxicFi7h, this sounds like a problem with your code rather than n8n. From taking a look at the Node.js documentation you probably want to do something like this:

const url = require('url');
let params = new url.URLSearchParams('abc=foo&def=%5Basf%5D&xyz=5');

Also, the Function Item node would need to return a JSON object, for example by running something like this:

return {
  myParameter: params.get('abc')

Here’s an updated workflow doing the trick:


Hope this helps!

1 Like

It led me to find this useful snippet

// Code here will run once per input item.
// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.functionItem
// Tip: You can use luxon for dates and $jmespath for querying JSON structures

let qs = item.data || location.search.slice(1);

var pairs = qs.split('&');
var result = {};
pairs.forEach(function(p) {
    var pair = p.split('=');
    var key = pair[0];
    var value = decodeURIComponent(pair[1] || '');
    if( result[key] ) {
       if( Object.prototype.toString.call( result[key] ) === '[object Array]' ) {
            result[key].push( value );
        } else {
            result[key] = [ result[key], value ];
    } else {
        result[key] = value;
return JSON.parse(JSON.stringify(result));

I’m facing the same issue as the @ToxicFi7h was facing

Same code does not work here :confused: