# JSON type automatically converted to string

**URL:** <https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142>\
**Category:** Developing Nodes\
**Created:** [31 July 2021 12:28 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142 "2021-07-31T12:28:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hkayan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hkayan/32/42713_2.png) [@hkayan](https://discourse.nodered.org/u/hkayan)\
**Post date:** [31 July 2021 12:28 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142/1 "2021-07-31T12:28:04Z")

</div>

Hi,

In my node-properties, I have defined the property as JSON object:

```auto
oneditprepare: function() {
            $("#node-input-subscribe").typedInput({
                type:"json",
                types:["json"]
            });
        }

```

This is how I define in HTML:

```
    <label for="node-input-subscribe"><i class="fa fa-spinner"></i>&nbsp;Subscribe</label>
    <input type="text" id="node-input-subscribe">

```

Then I get the input value in my `node.js` file as:

```auto

RED.nodes.createNode(this,config);
        node.config = config;
        node.subscribe = config.subscribe;

```

However when I send `node.subscribe` to Node-RED what I get is `string` not an object. I am checking via `debug `node:

```auto
msg : string[8]
"{"service":1453}"

```

What I am missing here?

Edit: Now I see, in the [docs](https://nodered.org/docs/api/ui/typedInput/#options-types), it is mentioned as

> a valid JSON string

So it is not converted, it is a JSON string by default. So, is there any way to get object?

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [31 July 2021 13:02 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142/2 "2021-07-31T13:02:57Z")

</div>

You will have to run JSON.parse(string) to convert it to an object. Be warned, JSON.parse can throw an error if the string cannot be parsed and that could crash node-red. You might want to wrap it in a try - catch block.

> **[JSON.parse() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)**
>
> The JSON.parse() method parses a
> JSON string, constructing the JavaScript value or object described by the string. An
> optional reviver function can be provided to perform a transformation
> on the resulting object before it is returned.

---

<div class="post-metadata">

**Author:** ![hkayan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hkayan/32/42713_2.png) [@hkayan](https://discourse.nodered.org/u/hkayan)\
**Post date:** [31 July 2021 13:34 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142/3 "2021-07-31T13:34:53Z")

</div>

Thanks @kevinGodell, this is what I am doing right now. That means there is no way to send object right?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 July 2021 13:51 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142/4 "2021-07-31T13:51:01Z")

</div>

Run the JSON.parse() inside your node before sending it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 August 2021 13:51 UTC](https://discourse.nodered.org/t/json-type-automatically-converted-to-string/49142/5 "2021-08-14T13:51:22Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
