# Converting string to json object in function node

**URL:** <https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338>\
**Category:** Questions\
**Created:** [November 2, 2019, 11:46am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338 "2019-11-02T11:46:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoamaral](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/brunoamaral/32/130_2.png) [@brunoamaral](https://community.n8n.io/u/brunoamaral)\
**Post date:** [November 2, 2019, 11:46am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/1 "2019-11-02T11:46:01Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [November 2, 2019, 12:07pm UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/2 "2019-11-02T12:07:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [November 2, 2019, 12:19pm UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/3 "2019-11-02T12:19:26Z")

</div>

Also published it as example here:

> **[n8n](https://n8n.io/workflows/156)**
>
> n8n is an extendable workflow automation tool which enables you to connect anything to everything via its open, fair-code model.

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/brunoamaral/32/130_2.png) [@brunoamaral](https://community.n8n.io/u/brunoamaral)\
**Post date:** [November 2, 2019, 2:37pm UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/4 "2019-11-02T14:37:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [November 2, 2019, 4:09pm UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/5 "2019-11-02T16:09:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [November 4, 2019, 6:56am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/6 "2019-11-04T06:56:05Z")

</div>

Did it help or do you still have problems?

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/brunoamaral/32/130_2.png) [@brunoamaral](https://community.n8n.io/u/brunoamaral)\
**Post date:** [November 4, 2019, 7:23am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/7 "2019-11-04T07:23:08Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [November 4, 2019, 7:28am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/8 "2019-11-04T07:28:12Z")

</div>

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 😉 If you found one, would be really great to get a workflow where it can be reproduced that I can fix it asap.

Thanks!

---

<div class="post-metadata">

**Author:** ![userX](https://avatars.discourse-cdn.com/v4/letter/u/c5a1d2/32.png) [@userX](https://community.n8n.io/u/userX)\
**Post date:** [July 17, 2023, 10:35am UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/9 "2023-07-17T10:35:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TAN\_YONG\_SHENG](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tan_yong_sheng/32/30275_2.png) [@TAN\_YONG\_SHENG](https://community.n8n.io/u/TAN_YONG_SHENG)\
**Post date:** [December 28, 2023, 3:31pm UTC](https://community.n8n.io/t/converting-string-to-json-object-in-function-node/338/10 "2023-12-28T15:31:00Z")

</div>

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

```auto
// 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();

```
