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.

Also published it as example here:

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:

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

This post is still the top result for searching for JSON conversion but since the Code block has been changed the sample isn’t working, can you update this post with a latest working example ?

PS.: It’s a pretty common use-case where the return data isn’t what n8n think it is, html rather then text or json rather then text, string rather then number, maybe a convert module would make sense, or the SET module would have an option to change data type, or something similar.

The code used for JSON conversion in the Code block as below:

// Loop over input items and add a new field called 'myNewField' to the JSON of each one
for (const item of $input.all()) {
  item.json.stdout = JSON.parse(item.json.stdout);
}

return $input.all();