Access specific json property from earlier node in function item

Hi,

I’ve got a function item within which I’m trying to set a variable which is pulled from an earlier set node.

const uuid = require('uuid');
item.uuid = uuid.v4();
item.processed_flag = 0;
item.batch_id = $item("Set batch ID");

// You can write logs to the browser console
console.log('Done!');

return item;

I’m able to pull the full item using

 item.batch_id = $item("Set batch ID");

But ideally I would be getting the specific json element called “batch_id”.

Is this possible with a function item node?

Thanks
Scott

the same as if you were doing it in an expression, just without the curly braces:

$node['the other node name'].json.batch_id
3 Likes

Thanks @mattesilver.

Not sure why, but that didn’t quite work, but it set me on the right course to come up with the following which did:

item.batch_id = $item(0).$node["Set batch ID"].json["batch_id"];

I’m doing this within a Function Item - not sure if that has anything to do with it, but it works now anyway.

Regards
Scott

2 Likes

ah, my version only works in function node, not function item

Seems broken on 0.153.0 :frowning:

Oh, I’m sorry to hear that @mattesilver. Expressions shouldn’t break with a new version. Can you provide an example workflow that’s working with version <0.153.0 but no longer does now?

When testing the above expression, it’s still working for me like so:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "batch_id",
              "value": "123"
            }
          ]
        },
        "options": {}
      },
      "name": "Set batch ID",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.my_batch_id_field = $item(0).$node[\"Set batch ID\"].json[\"batch_id\"];\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set batch ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set batch ID": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I’ll have a closer look and report an issue in gitlab if I confirm it.

Hmm,

Can’t reproduce any more.

I did have issues when image was rebuilt and I didn’t force-refresh browser window, so maybe that was that.

Anyway, both versions work for both function and function item nodes.

console.log($node['MyConfig'].json['key']);
console.log($item(0).$node["MyConfig"].json["key"]);

Cheers

1 Like

Thanks @mattesilver, glad to hear this is now working for you, thanks a lot for confirming!

It has failed again in my production workflow, but I’ve figured it out.
The short version, without $item(0), takes the other nodes output item with the current index.
That means, when my function node handles 99th item, and the configuration node only outputs 1, it fails because it doesn’t find the 99th item in the config node output.

1 Like