Get array of quoted strings for JSON

I’m working on periodical data synchronization between LDAP catalog and MongoDB collection. The logic to do it very simple: at first, we need to insert/replace all existing LDAP user records in MongoDB and then delete from it records which no longer exist in LDAP.

To delete records, I need to pass a quoted list of UIDs to mongo json query. I try to do it with $jmespath function, but it looks n8n forcibly removes quotes from the $jmespath output (this behavior is adopted under another question):


At the same time, a preview of the expression shows the result that I need:


I understand that the question of expected behavior is debatable. Is there any way to get an array of quoted strings for JSON without explicit generation of it in the JS code node?

Example workflow

n8n setup

  • n8n version: 0.230.3
  • Database: SQLite
  • n8n EXECUTIONS_PROCESS setting: own, main
  • Running n8n via: Docker
  • Operating system: MacOS

Hi @nixikanius

I’m the author of the mentioned question.

Here’s what should work for you:

Change your expression to

{ "uid": { "$nin": {{ JSON.stringify($jmespath($input.all(), "[*].json.uid")) }} } }


Everything between double-curly-brackets in the expression editor is considered javascript. The $jmespath()-function constructs an array. The array is embedded inside text:

"{ “uid”: { “$nin”: [ Array here ] } }

At some point, n8n seems to get confused, merging the two. In the preview, it extrapolated correctly, in the actual request, it’s probably doing something else (sanitization/…? haven’t looked at the code).

The above solution works, because it creates a json (text) representation of the array and embeds it into the surrounding text. Square brackets included.

For simple nodes, I now always make sure the expressions in my n8n workflows output text-only from the constructing javascript. For everything else, I anyway prefer the code node. No issues since.

Don’t embed a JS-Object into text that is part of a node expression. n8n might not do with it what you expect. Instead, toString() first.

Hope this helps!
ps: haven’t tested your exact case, ymmv. let me know.


@sdsys thank you, using of JSON.stringify works as expected.

The only one problem for n8n team left: the difference between $jmespath preview (it is with quotes) and real result (it is without quotes).

1 Like

Glad this solved your issue :+1:t3:

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