Can someone please point me to a tutorial or examples for dealing with JSON objects sent from Node Red in groovy? I have a couple of Roombas that I like to display the mission status on my dashboard. Right now, I have it all working great with individual global variables and NR. I am new to Groovy and the syntax therein so as a learning exercise I wanted to design a more elegant/efficient solution to be able to remove the global variables and the individual connections to NR. I got as far as creating a custom driver that has the attributes I need and updated NR so that I can remove the global variables; that all works fine. However, now I want to remove the individual NR connections and have only one connection that sends the entire Roomba status message and then parse it in the groovy driver. I think I have NR sending the whole message (the log.info reads [object Object]) when I send the message but that's where I'm stuck, I can't find the correct tutorial/example(s) that show me how to...
- View/log the message as it is (so I can see if I'm sending from NR correctly).
- Parse the message when I verify what the message structure is.
Any guidance would be appreciated, thanks!
Have you taken a look at JsonSlurper? Parses a string into a data structure.
Yes, I have. I haven't gotten that far yet as whenever I try to use slurper my Groovy driver errors out, so I think I'm not sending the data from NR properly; which is why I need to figure out how to view the raw message first. The whole structure comes from my Roomba as the msg.payload object, I move that to the msg.arguments object to be used with the Command node and send it to HE so I must be doing something wrong.
This is my simple groovy command function:
and as soon as I inject my NR this is the error I get:
EDIT: If I just log.info the stat object I get [object Object] but no error.
Is it just passing the string "[object Object]"? You have to serialize it -JSON.stringify(msg) on the Node side using a function node. Just a guess.
The message itself is an object...
I take this object's msg.payload and move it to msg.arguments for the HE Command Node that the Maker API uses, that's it. Am I to assume that this object isn't a JSON object that can be passed directly to HE and I need to convert it?
EDIT: I was under the impression that the msg object in NR was already a JSON object.
The error you are getting points to the "o" in the literal string "[object Object]" at least that's what it looks like to me - nothing is getting transferred over except that string.
Unless I'm missing something you need to "serialize" the NR object (turn it into a string) before it gets sent so your HE driver can parse it back into a JSON object it can use.
OK, I'll give it a try, can't hurt. From what I read the Maker API supports JSON objects, I just didn't know/read that it wasn't natively as an 'object'.
Thanks @erktrek! That worked out perfect. I got the whole message and then the simple 'dot' syntax worked to pull out the data from the object. Thanks again.
For any fellow noobs in a similar situation, not necessarily Roomba, but any msg.payload object that you want to get into HE ...
In NodeRed use the 'json' node to convert any msg object into a stringified version. Put that into msg.arguments with a 'change' node. Use the HE command node to get that packet into an HE JSON Object. From there you can parse/manipulate away.