Node-red,Ubuntu, JSON parse - unexpected token - issue

1,533 views
Skip to first unread message

bronto....@gmail.com

unread,
Nov 6, 2017, 2:50:24 PM11/6/17
to Node-RED

Hello

I intalled the node-red in Linux Ubuntu.
I have an issue with json string parse to object.
Below some informations.

node -v
8.9.0

npm -v
5.5.1

MQTT - mosquitto - publish test json format.

{"dht22":{"temperature":{"value": 25.4},"humidity":{"value": 40}}}

~# mosquitto_pub -d -u user -P userpass -t /sensor/test -m "{"dht22":{"temperature":{"value": 25.4},"humidity":{"value": 40}}}"


The flow

debug
msg : string[40]"Unexpected token d in JSON at position 1"

Could anyone help me with that issue, some suggestion how to find the reason?

Best regards,
Bronto



Auto Generated Inline Image 1

Nick O'Leary

unread,
Nov 6, 2017, 2:54:38 PM11/6/17
to node...@googlegroups.com

You have a debug node attached to your mqtt node. What does it show? That will tell us exactly what payload you're passing the json node and from there what is causing the error.

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+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.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/c3db2ffc-8e3c-45aa-b08d-8e9b0cd11a68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bronto....@gmail.com

unread,
Nov 6, 2017, 3:04:01 PM11/6/17
to Node-RED

Debug form mqtt node and json


6.11.2017, 20:58:36node: b627264b.c189e8/sensor/test : msg.payload : string[56]
"{dht22:{temperature:{value: 25.4},humidity:{value: 40}}}"
 
6.11.2017, 20:58:36node: 5a47905.5b95b7
msg : string[40]
"Unexpected token d in JSON at position 1"








 

 
Auto Generated Inline Image 1

Nick O'Leary

unread,
Nov 6, 2017, 3:10:35 PM11/6/17
to node...@googlegroups.com

You are not sending valid json:

{dht22:{temperature:{value: 25.4},humidity:{value: 40}}}

None of the property names had quotes around it.

It needs to be:

{"dht22":{"temperature":{"value": 25.4}, "humidity":{"value": 40}}}

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+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.

bronto....@gmail.com

unread,
Nov 6, 2017, 3:32:40 PM11/6/17
to Node-RED

Thank You very much Nick

In Linux, I have now made that command:
#mosquitto_pub -d -u user -P userpass -t /sensor/test -m "{\"dht22\":{\"temperature\":{\"value\": 25.4},\"humidity\":{\"value\": 40}}}"
With backslash, and now I have correct value:

6.11.2017, 21:20:55node: b627264b.c189e8/sensor/test : msg.payload : string[66]
"{"dht22":{"temperature":{"value": 25.4},"humidity":{"value": 40}}}"
6.11.2017, 21:20:55node: bf10bbf3.2f2588
/sensor/test : msg.payload : Object
{ dht22: object }


Best regards,
Bronto

Auto Generated Inline Image 1

Neil Cherry

unread,
Nov 7, 2017, 8:01:07 AM11/7/17
to Node-RED
On Monday, November 6, 2017 at 3:32:40 PM UTC-5, bronto....@gmail.com wrote:

Thank You very much Nick

In Linux, I have now made that command:
#mosquitto_pub -d -u user -P userpass -t /sensor/test -m "{\"dht22\":{\"temperature\":{\"value\": 25.4},\"humidity\":{\"value\": 40}}}"
With backslash, and now I have correct value:

Bronto, sorry I didn't see this earlier. Another method of sending the data from the command line:

 mosquitto_pub -d -u user -P userpass -t /sensor/test -m '{"dht22":{"temperature":{"value": 25.4},"humidity":{"value": 40}}}'

Note that there are single quotes around the message string. Quoting from the command line can lead to some interesting problems.

Also I don't think you need the leading slash on the topic. I recall that the leading slash can cause issue. I can't recall why at the moment.

bronto....@gmail.com

unread,
Nov 8, 2017, 7:05:53 AM11/8/17
to Node-RED

Nail thanks

It's working.
I was sending with single quotes - json was correct.
Now I'll be playing with real sensor.

Thank you all
Best regards
Bronto


Michael Hogan

unread,
Nov 8, 2017, 1:47:45 PM11/8/17
to Node-RED
To debug stuff like this, and to debug certain aspects of complex Function scripts, I usually use the Chrome debugger.

The one major drawback to this technique is that we generally develop for the Pi, and the standard installation of NR on Raspbian Jessie seems to use a JavaScript engine that is pretty old .. so a lot of ES6 syntactical goodness is unavailable, and occasionally newer, incompatible syntax creeps into the code as it gets revised .. so we occasionally need to weed stuff a bit when the code is moved back to the Pi's NR context.

But .. it beats logging and message debug blocks when chasing a complex issue.

Nick O'Leary

unread,
Nov 8, 2017, 2:05:42 PM11/8/17
to node...@googlegroups.com

> standard installation of NR on Raspbian Jessie seems to use a JavaScript engine that is pretty old 

Which is one reason why our Pi docs recommend you run our update script to get to latest stable node/npm/Node-RED versions.

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+u...@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Michael Hogan

unread,
Nov 9, 2017, 12:31:18 AM11/9/17
to Node-RED
Not worth the hassle .. we ride the version train where it takes us. Configuration management is hard enough without adding the risk of extra gotchas in exchange for meager benefits.

Last time I went through that process, everything worked, except for the serial blocks .. which we use extensively. I'm sure I could have fixed it, but I have a dozen other things to do on any given day (patent stuff, licensing stuff, investor meetings, legal stuff, CAD stuff etc. etc.) .. the more I screw with the platform, the less it's worth to me.

Colin Law

unread,
Nov 9, 2017, 3:04:46 AM11/9/17
to node...@googlegroups.com
You can't complain in one post about the problems of using out of obsolete tools and in the next say you are not prepared to upgrade.

Colin

On 9 Nov 2017 5:31 a.m., "Michael Hogan" <mikeh...@gmail.com> wrote:
Not worth the hassle .. we ride the version train where it takes us.  Configuration management is hard enough without adding the risk of extra gotchas in exchange for meager benefits.

Last time I went through that process, everything worked, except for the serial blocks .. which we use extensively.  I'm sure I could have fixed it, but I have a dozen other things to do on any given day (patent stuff, licensing stuff, investor meetings, legal stuff, CAD stuff etc. etc.) .. the more I screw with the platform, the less it's worth to me.

--
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 an email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Michael Hogan

unread,
Nov 10, 2017, 12:23:57 AM11/10/17
to Node-RED
I didn't complain .. I just observed that, after working out a solution in a newer flavor of ES that there is occasionally a additional step of figuring out how to do it with a slightly older flavor, and observed further that Jessie seems a step behind.

Then Nick offered that the guidelines suggest upgrading, and I offered that it didn't make sense for us.

I guess I should have said "minor drawback" rather than "major drawback".

Reply all
Reply to author
Forward
0 new messages