JSON type automatically converted to string

Hi,

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

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:


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:

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

What I am missing here?

Edit: Now I see, in the docs, 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?

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.

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

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

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