Jmespath filter in code node not working

Describe the issue/error/question

I am using the jmespath command that works on JMESPath Query Tester:


When I use it in the code node it gives no output.

What is the error message (if any)?

No error message, just no output.

Please share the workflow

Share the output returned by the last node

No output data returned

Information on your n8n setup

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

You probably forgot about the data structure n8n uses. That all data is underneath json. You can find more information about that here.

Meaning in the query you also have to account for the json key and so look like this:

var list = $input.all();
var shortlist = $jmespath(list, "[?json.Name == 'Ingrid']");
return shortlist;

@jan thanks for pointing this out! … now finally the penny has dropped for me :slight_smile: … what you see is not actually what you get :wink:

And please correct me if I’m wrong, when the JSON displayed had multiple items (IN THIS CASE 3), i.e.:

[
  {
    "Activity": "Fitness Kurs 1",
    "Trainer": "Monika",
    "Name": "Inge"
  },
  {
    "Activity": "Fitness Kurs 1",
    "Trainer": "Monika",
    "Name": "Eveline"
  },
  {
    "Activity": "Fitness Kurs 2",
    "Trainer": "Anna",
    "Name": "Ingrid"
  }
]

Then the data in the n8n background is:

[
  {
    "json": {
      "Activity": "Fitness Kurs 1",
      "Trainer": "Monika",
      "Name": "Inge"
    },
    "pairedItem": {
      "item": 0
    }
  },
  {
    "json": {
      "Activity": "Fitness Kurs 1",
      "Trainer": "Monika",
      "Name": "Eveline"
    },
    "pairedItem": {
      "item": 1
    }
  },
  {
    "json": {
      "Activity": "Fitness Kurs 2",
      "Trainer": "Anna",
      "Name": "Ingrid"
    },
    "pairedItem": {
      "item": 2
    }
  }
]

And that with the same (DISPLAYED) data, but only 1 item, the data in the background is:

[
  {
    "json": [
      {
        "Activity": "Fitness Kurs 1",
        "Trainer": "Monika",
        "Name": "Inge"
      },
      {
        "Activity": "Fitness Kurs 1",
        "Trainer": "Monika",
        "Name": "Eveline"
      },
      {
        "Activity": "Fitness Kurs 2",
        "Trainer": "Anna",
        "Name": "Ingrid"
      }
    ],
    "pairedItem": {
      "item": 0
    }
  }
]

NOTE: the data displayed looks exactly the same. Only the number of items indicates whether it’s an array or object.

I found out, that you can actually see what’s in the background by feeding the data into a Code node with the following code:

return [$input.all()]

This now finally explains a lot (if not everything) to me, and I keep wondering how I could have missed this essential piece of information …

1 Like

Hi Jan, this works now perfectly! Thanks for the fast reaction.
br,
Patrick

1 Like

Great to hear that it helped @pghahrem !

Also good to hear that it was helpful for you @dickhoning. What you wrote is almost correct. The only thing that is not correct is the example for “And that with the same (DISPLAYED) data, but only 1 item, the data in the background is” is not correct.
The reason is that by definition (as that is how n8n internally expects the data and how everything is written), the json key has to contain an object and is not allowed to be set to an array. Meaning the array would have to be underneath a key (for example data), and would so look like this:

[
  {
    "json": {
      "data": [
        {
          "Activity": "Fitness Kurs 1",
          "Trainer": "Monika",
          "Name": "Inge"
        },  
        {
          "Activity": "Fitness Kurs 1",
          "Trainer": "Monika",
          "Name": "Eveline"
        },
        {
          "Activity": "Fitness Kurs 2",
          "Trainer": "Anna",
          "Name": "Ingrid"
        }
      ]
    },
    "pairedItem": {
      "item": 0
    }
  }
]
1 Like

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