msg.payload : string to msg.payload :object

5,920 views
Skip to first unread message

Javier Sánchez Buitrago

unread,
May 9, 2017, 6:58:06 AM5/9/17
to Node-RED
Hi all, I'm try to connect some sensors, that I have acess throught an API. I'm use the module http request and works well but I have 2 problems.
If I want return the message like a JSON the debug saids: JSON parse error, for this reason I choose the return as string, and this return exactly this:
msg.payload : string [135]
{"Id":80,"PidType":133,"Key":"CANCELLED","Name":"Product cancelled","Description":"Product cancelled","IsAlarm":0,"Value":4,"owner":4}

Here the second problem, I need that the msg be a object, to handle it as a JSON

If can help me, I will grateful with you.

Thanks.

Glenn

unread,
May 9, 2017, 7:12:25 AM5/9/17
to Node-RED
If you can't use the JSON node. You can also use JSON.parse in a function.

That looks like valid JSON to me.

Both methods will conver it to an object

Javier Sánchez Buitrago

unread,
May 9, 2017, 7:51:59 AM5/9/17
to Node-RED
If I build a function with JSON.parse as:
var newMsg = JSON.parse(msg.payload);
return newMsg;

the result is:
function : (error)SyntaxError: Unexpected token

steve rickus

unread,
May 9, 2017, 9:41:55 AM5/9/17
to Node-RED
Since the JSON node is using the same parse() function, it's not surprising that neither of them work with your data. This indicates a problem with your data...

When I copy/paste the string from your previous email, it does indeed parse correctly -- however, it is only 134 characters long. Your original post shows msg.payload : string [135] so there must be a non-printable character in the string coming out of the API call. Normally, trailing line breaks or white space should not affect the JSON parser, but you could try to trim the string before parsing it:

var data = msg.payload;
console
.log("payload data: length = " + data.length);
data
= data.trim();
console
.log("trimmed data: length = " + data.length);

msg
.payload = JSON.parse(data);
return msg;

(Generally, you should return the original msg with its new payload -- this preserves any other msg fields for other downstream nodes to use)

If the data parses correctly, the server console should show that the trimmed data length is different than the original payload length. If not, and it still throws an error, there must be an invalid character embedded in your string. Finding that will require some more inspection of the original string, but let's see if this works first.
--
Steve

Javier Sánchez Buitrago

unread,
May 10, 2017, 4:08:30 AM5/10/17
to node...@googlegroups.com
Thos convert to Object well!
Thanks Steve!

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/8Av3EyMQ0o0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/b2fd8ea9-d573-4fae-b0ee-638b8a066a91%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

steve rickus

unread,
May 10, 2017, 9:17:38 AM5/10/17
to Node-RED
May I ask -- what is logged in the console? Did the length of the input string change before and after using the trim() function?

If the string will not parse correctly unless it is trimmed of some funny characters, then it would be good to know so someone else does not have this same problem later. Could you add a file out node to your flow, put the untrimmed data into a temporary file, and post the file here so we can test it?

Nick O'Leary

unread,
May 10, 2017, 9:33:26 AM5/10/17
to Node-RED Mailing List
I've seen this in the past. It is often caused by the Byte order mark character (U+FEFF) that some text editors prepend to a file. 

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
May 10, 2017, 9:37:49 AM5/10/17
to Node-RED
Ah, brilliant! So some http responses are not parsable because of this -- Is that something the json node and http-request node can check for? And if the trim() function fixes it, is there any reason not to use that wherever JSON.parse() is called?


On Wednesday, May 10, 2017 at 9:33:26 AM UTC-4, Nick O'Leary wrote:
I've seen this in the past. It is often caused by the Byte order mark character (U+FEFF) that some text editors prepend to a file. 

Nick
On 10 May 2017 at 14:17, steve rickus <sri...@gmail.com> wrote:
May I ask -- what is logged in the console? Did the length of the input string change before and after using the trim() function?

If the string will not parse correctly unless it is trimmed of some funny characters, then it would be good to know so someone else does not have this same problem later. Could you add a file out node to your flow, put the untrimmed data into a temporary file, and post the file here so we can test it?


On Wednesday, May 10, 2017 at 4:08:30 AM UTC-4, Javier Sánchez Buitrago wrote:
Thos convert to Object well!
Thanks Steve!

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Dave C-J

unread,
May 10, 2017, 12:54:15 PM5/10/17
to node...@googlegroups.com
Seems sensible to me (at first glance)... 

Nick O'Leary

unread,
May 10, 2017, 1:14:17 PM5/10/17
to node...@googlegroups.com

But in practice, the parsing for the http nodes is done deep within the underlying modules. Not code we'd want to do ourselves for this one very rare edge case.

Nick


On Wed, 10 May 2017, 17:54 Dave C-J, <dce...@gmail.com> wrote:
Seems sensible to me (at first glance)... 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
May 10, 2017, 2:02:28 PM5/10/17
to Node-RED
That make sense -- however, the http-request node info says Tip: If the JSON parse fails the fetched string is returned as-is.

So in that specific case where the underlying module parsing failed, we still have the raw text, right? Could that trim() and parse() be attempted in node-red just before building/returning the response msg?

Nick O'Leary

unread,
May 10, 2017, 2:04:12 PM5/10/17
to Node-RED Mailing List
there's a long standing issue that gets in the way here: https://github.com/node-red/node-red/issues/676

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
May 10, 2017, 2:54:57 PM5/10/17
to node...@googlegroups.com
Ah right yes.. (but still possibly useful for the JSON node ??)

Reply all
Reply to author
Forward
0 new messages