Pass array to TypedInput type "json"

Hi folks,

I have a TypedInput of type "json", and I pass a default array to it:

But when I open the JSON editor, the array is considered incorrect:

image

So the quotes and rectangular brackets are gone.
Must be something stupid, but need a fresh pair of eyes to look at this code snippet ...

Thanks !!
Bart

Is as the value really a Javascript object allowed or only a stringified JSON?

If the type is JSON, then it expects it's value to be a JSON string, not a JavaScript array

@Hypnos,
I was wondering the same thing last night, because the name "json" already tells everything. But since Node-RED is Javascript objects all over the place, I thought that it had to be a Javascript array (and me using it somehow incorrectly)...
And I was so sure that I already had used it before, but couldn't find anything...

@knolleary: thanks for the explanation and confirmation!

If anybody is ever searching for it:

  • Set the "json" typedInput value:
    myInputField.typedInput("value", JSON.stringify(value || []));
    
  • Get the "json" typedInput value:
    var value = JSON.parse(myInputField.typedInput('value'));
    
1 Like

Not really Bart, you should use RED.util.evaluateNodeProperty which will parse the JSON according.

1 Like

Ah yes Steve,
That is why I couldn't find it anymore. After a heavy day at work completely forgotten about that one...
Every time I look at that functions parameters, I always have to remind myself whether this is used for node or msg fields or both.

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