Execute Command $items?

Following the recommendation to use Execute Command when trying to write scripts in anything else but node, I build my own version of the docker image with my preferred runtime included, and now at the point where I try to access the data from the previous node, just like in Function the items, or in Function Item the item ( node ).
Is the data available in some environment variable or file location?

Welcome to the community @user23

So you want to access data from a previous node if the execute command? If so, you can use expressions

That looks “almost” good enough, with Expressions you can get a selected field of one of the item of the input data, so like “json[0].field”, where what I’m looking for is “json”.

Hey @user23!

If you want the whole JSON object, you can refer to the json, don’t specify any fields. Let me know if you need more explanation :slight_smile:

Yes @harshil1712 I do need more explanation
echo “{{$json}}”
give me only
[object Object]

In edit view it looks OK… but when saved it breaks.

Here is a full workflow for example.

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://github.com/n8n-io/n8n/releases.atom"
      },
      "name": "RSS Feed Read",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1,
      "position": [
        530,
        310
      ]
    },
    {
      "parameters": {
        "command": "=echo \"{{$json}}\""
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        730,
        310
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "RSS Feed Read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RSS Feed Read": {
      "main": [
        [
          {
            "node": "Execute Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {}
}

You will have to convert the JSON object to a string which will let the Shell print it. You can use the JSON.stringify() method to do that.

Here’s the updated workflow:

{
  "nodes": [
    {
      "parameters": {
        "url": "https://github.com/n8n-io/n8n/releases.atom"
      },
      "name": "RSS Feed Read",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1,
      "position": [
        743,
        320
      ]
    },
    {
      "parameters": {
        "command": "=echo {{JSON.stringify($json)}}"
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        943,
        320
      ]
    }
  ],
  "connections": {
    "RSS Feed Read": {
      "main": [
        [
          {
            "node": "Execute Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I tested the workflow you posted, and that does address the single property per item issue, but still just one item. So now it’s we got from json[0].field up till json[0], but still not down to json.

So in this example, the “Execute Command” will have only one of the release ( the latest ), not the full data. ( comparing to a Function where I can use return items and I get all the releases )

If you want to return all the items, then in that case you might have use $items(). Here’s the documentation that might be helpful for you: Expressions | Docs

As I know almost nothing about node.js it’s quite a hard guess game at this point, but I’m getting closer.

cat << EOF
{{$evaluateExpression(JSON.stringify($items()))}}
EOF

This work at least up to the point of getting me back everything, even if the structure is messed up.
If I manage to get the original json that would be fabulous otherwise I will just go with this as it is.

$items() will give you the JSON. Can you please help me understand what exactly you mean by “Original JSON”? Do you want the exact response that the endpoint returns?

{{$evaluateExpression($items())}}

gives me

[object Object],[object Object]....

{{$evaluateExpression(JSON.stringify($items()))}}

gives me

[ 
  {"json":
    {"title":"[email protected]",
    "link":"https://github.com/n8n-io/n8n/releases/tag/n8n%400.125.0"....}},
  {"json":
    {"title":"[email protected]",
    "link":" ....

What I’m looking for, or just hoped for at least, is the source data in json, and not in node.js json object.
Something like:

[ 
    {"title":"[email protected]",
    "link":"https://github.com/n8n-io/n8n/releases/tag/n8n%400.125.0"....},
    {"title":"[email protected]",
    "link":" ....

The other way would be to wrap them in an array and create a single item and refer to this item. You can check the code snippet for modifying the structure here: JavaScript Code Snippets | Docs

Wow that took much longer then I expected, and It’s way more complicated then I ever imagined, but the important thing is that it works.
There is a sample workflow which implement the basic functionality.

{
  "name": "Raw data pass-on",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        630,
        330
      ]
    },
    {
      "parameters": {
        "url": "https://github.com/n8n-io/n8n/releases.atom"
      },
      "name": "RSS Feed Read",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1,
      "position": [
        822,
        330
      ]
    },
    {
      "parameters": {
        "command": "=cat << EOF\n{{JSON.stringify($json[\"data_object\"])}}\nEOF"
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1200,
        330
      ]
    },
    {
      "parameters": {
        "functionCode": " return [\n  {\n    json: {\n      data_object: items.map(item => item.json)\n    }\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1000,
        330
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "RSS Feed Read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RSS Feed Read": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Command": {
      "main": [
        []
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Execute Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "2"
}

@RicardoE105 @harshil1712 Thanks for the help!

1 Like