Closed colinl closed 4 years ago
Save your sanity and stick with JSON objects. I do try to handle JSON string that can be decoded to objects but I don't know if I cover all possible implementations of it.
Example: {"payload":"{ \"payload\":\"on\",\"timeout\":6,\"warning\":0}","qos":0} becomes: { "payload":"on","timeout":6,"warning":0}
But if it arrives in another format I'm not sure I can handle it correctly. If you mean something else please provide an example. Actually it would be a good idea to provide and example anyway.
I recall strings to object giving me nightmares figuring out and testing.
I am confused by your use of the term "JSON object". JSON is a method of encoding javascript objects as strings (https://en.wikipedia.org/wiki/JSON), so there is not really any such thing as a JSON object. There are javascript objects and there are JSON strings. JSON is always a string, which represents a javascript object. Am I right in thinking that when you say JSON object you actually mean javascript objects?
Sorry, I don't write as much code as I used to (Python for automation, C, Asm). The rest is hobby so I'm not up on the correct terminology (I do QA and know correct terminology is important). I also see I messed up the quoting above. What I meant by a JSON string is a string that contains escaped JSON (it has not been parsed yet): Example:
{"payload":"{ \"payload\":\"on\",\"timeout\":6,\"warning\":0}","qos":0} // the payload contains a string
Parsed becomes:
{ "payload":"on","timeout":6,"warning":0 } // This is JSON, I call it a JSON object
I've been calling what the first payload returns a 'JSON string' and the second a JSON object. Technically it's all strings but when you query (typeof(msg.payload) it from Javascript it's a string and an object
If, for example, in a function node I put
msg.payload = {"payload":"on", "timeout":6, "warning":0 }
return msg
Then in msg.payload is a javascript object, not a JSON object. So to get back to my original question, can I feed the output of the above function into your node and expect it to work?
msg.payload = {"payload":"on", "timeout":6, "warning":0 } return msg
Yes, that will work.
I basically get this from an inject (payload {} "payload":"on","timeout":30} ):
{"_msgid":"7693409.f2669c","topic":"","payload":{"payload":"on","timeout":30}}
That's from a debug node set to the complete msg object. Note the lack of the warning means it takes the default values. Same would apply to the timeout. I think I treat a lack of a payload as an on message. The original timer that Pete wrote just send something to the time to 'tickle' it on.
Sorry for any confusion.
Thanks for clarifying that the node accepts Javascript objects rather than JSON (string). Could I suggest that you remove references to JSON from the readme as it causes confusion among readers who think that it is necessary to pass in a string rather than just ordinary javascript objects with attributes. See this thread on the node-red forum for example where the poster was having difficulty trying to construct a string. https://discourse.nodered.org/t/need-some-help-with-a-varable-json/14210
Thanks for the information, I'll read that and get the readme corrected. :-)
This won't be easy.
Colin thanks for the information and setting me straight. I've made a first attempt at the corrections. I hope this is better. I still need to further work so that it is less confusing.
Oh, having a bit of trouble with github push (dang key issue, I'll resolve that this weekend). The NPM publish worked better.
I'm going to close this but I still have additional work on the documentation to do.
The documentation says that we can pass in JSON containing attributes such as payload and timeout. Does this actually mean that I have to construct a JSON string containing the structure or should it be a javascript object containing the values?