Converting string to json object in function node

This is mostly due to my lack of knowledge in JS and NodeJS.

I have an “Execute Command” node that returns json in stdout; and I am trying to access it in another node to set a custom message if one of the properties is true.

So I am trying the following but it seems it only outputs the exact same data it received:

const input = $node["DL from Telegram"].data["stdout"];
const result = JSONparse(input)
return result;

I hope that helps. It emulates a command returning JSON and then shows an example of how to bring the data in the flow and another one on how to use the data directly.

{
  "nodes": [
    {
      "parameters": {
        "command": "echo \"{ \\\"value1\\\": true, \\\"value2\\\": 1 }\""
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        450,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "item = JSON.parse(item.stdout);\nreturn item;"
      },
      "name": "FunctionItem - To Flow Data",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{JSON.parse($node[\"Execute Command\"].data[\"stdout\"]).value1}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        500
      ]
    }
  ],
  "connections": {
    "Execute Command": {
      "main": [
        [
          {
            "node": "FunctionItem - To Flow Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Also published it as example here:
https://n8n.io/workflows/156

1 Like

Your example works, but for some reason I still don’t see any json data in mine.

My slimmed version of the workflow is this:

{
  "name": "test-yt-dl",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "48263287",
        "text": "=",
        "additionalFields": {}
      },
      "name": "Telegram1",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        950,
        300
      ],
      "credentials": {
        "telegramApi": "Johnny Five"
      }
    },
    {
      "parameters": {
        "functionCode": "item = JSON.parse(item.stdout);\nreturn item;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        300
      ],
      "color": "#FF0000"
    },
    {
      "parameters": {
        "command": "=wget -O - --post-data 'url={{$node[\"Set Url\"].data[\"url\"]}}&format=bestvideo' https://DOMAIN.me/youtube-dl/q;"
      },
      "name": "DL from Telegram",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "url",
              "value": "=https://www.youtube.com/watch?v=H5dTMYOHedY"
            }
          ]
        }
      },
      "name": "Set Url",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Telegram1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DL from Telegram": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Url": {
      "main": [
        [
          {
            "node": "DL from Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set Url",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "10"
}

In the meantime I am using the second example with IF.

Honestly, do not understand what you have planned. But here are three observations/questions which maybe help:

  1. The “wget” does not return JSON it returns HTML => What data do you want to get?
  2. Why do you use wget in the command-line instead of a HTTP Request-Node? => It should return the same data
  3. Even if it would return JSON it can not work because you use a Function-Node not like in the example a “Function Item”-Node

I hope that helps anyway even if I can not supply a working example as a response.

Did it help or do you still have problems?

It did help. Your comment about the HTTP module made me go back to it and realise I missed a step in configuring it the first time.
Since it returns JSON I was able to set the variable in the way I wanted.

I also found an issue when running the Function Node, it sometimes hangs when I am trying to set a variable using an if statement. I’ll see if I can gather more information about it and make sure it’s not a problem on my side. :slightly_smiling_face:

1 Like

Ah great to hear that it helped and you got it working!

Currently not aware of any issues there. Which sadly does not mean much like for any bug :wink: If you found one, would be really great to get a workflow where it can be reproduced that I can fix it asap.

Thanks!

1 Like