# Unexpected token D in JSON at position 0

**URL:** https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680
**Category:** General
**Created:** [12 April 2020 07:47 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680 "2020-04-12T07:47:26Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 April 2020 08:26 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/21 "2020-04-12T08:26:59Z")

</div>

Generally each topic should be used for a particular piece of information, the value of a particular sensor, a status string about something and so on. It is not a good idea to have a general 'out' topic that is used to send different types of information.

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [12 April 2020 08:27 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/22 "2020-04-12T08:27:38Z")

</div>

Well the quick solution would be to send the original message in a JSON format, otherwise I have to change all my flows. The domoticz/out topic also receives messages from devices in domoticz (which are all in JSON format) and I need some device messages as well in this particular flow

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [12 April 2020 08:30 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/23 "2020-04-12T08:30:31Z")

</div>

> [@Colin](#):
>
> Generally each topic should be used for a particular piece of information, the value of a particular sensor, a status string about something and so on. It is not a good idea to have a general 'out' topic that is used to send different types of information.

But can I use 2 mqtt nodes in one flow? one for JSON and one for String messages then?

after this I want to create a function node which does something like:

```auto
if (msg.payload.idx == '243') {
  if (msg.payload.nvalue == '1' && msg = 'De frietpan is aangezet!'){

  }
}

```

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [12 April 2020 08:34 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/24 "2020-04-12T08:34:47Z")

</div>

You can subscribe to one topic as often as you want. What @Colin says is right in an ideal scenario each topic would be for one purpose, eg a sensor or a light state. But I also have had the circumstances where I had to use one topic as a kind of wildcard where all information from another system arrives. So if it is hard for you to change this on the domotics side than you will have to do some filtering on the Nodered side.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [12 April 2020 08:40 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/25 "2020-04-12T08:40:03Z")

</div>

> [@gschmidt](#):
>
> if (msg.payload.idx == '243') { if (msg.payload.nvalue == '1' && msg = 'De frietpan is aangezet!'){ } }

This doesnt work. your treating your msg object like a string and a js object at the same time. This can not be.  
You are only sending a string or a json at a time right?

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [12 April 2020 08:42 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/26 "2020-04-12T08:42:04Z")

</div>

you can use a function node with try...catch and JSON.parse() instead of the JSON parser node to sort out the strings there.

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [12 April 2020 08:47 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/27 "2020-04-12T08:47:11Z")

</div>

Hoi Gerben,

De volgende links zijn de moeite waard om eerst te lezen, voordat je verder gaat.

> **[JSON](https://nl.wikipedia.org/wiki/JSON)**
>
> JSON of JavaScript Object Notation, is een gestandaardiseerd gegevensformaat. JSON maakt gebruik van voor de mens leesbare tekst in de vorm van data-objecten die bestaan uit een of meer attributen met bijbehorende waarden. Het wordt hoofdzakelijk gebruikt voor uitwisseling van data tussen server en webapplicatie, als een alternatief voor XML.
> JSON is oorspronkelijk ontstaan uit de programmeertaal JavaScript, maar is een taalonafhankelijk dataformaat. Code voor het lezen en maken van JSON-data is...

  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

Als een payload beschikbaar is als string in JSON formaat, de mqtt out met json setting, of json node, kan deze omgezet worden in een JavaScript object. Als de string niet in json formaat is, krijg je de error die je al gezien hebt.

Een andere tip is om je (debug) nodes een naam te geven, zodat je ze makkelijk terug kan vinden en weet welk bericht in de debug zijbalk waarbij hoort zonder het ID op te hoeven

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [12 April 2020 08:50 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/28 "2020-04-12T08:50:32Z")

</div>

> [@afelix](#):
>
> Als een payload beschikbaar is als string in JSON formaat, de mqtt out met json setting, of json node, kan deze omgezet worden in een JavaScript object. Als de string niet in json formaat is, krijg je de error die je al gezien hebt.

I already noticed, the easiest way is to change my dzVents script in domoticz to send the messages to MQTT in JSON format

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [12 April 2020 08:53 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/29 "2020-04-12T08:53:08Z")

</div>

heel goed 👍

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [12 April 2020 08:53 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/30 "2020-04-12T08:53:15Z")

</div>

Then please make sure that all messages published to the same topic arrive in the same format, you’d spare yourself a lot of work and mental struggles.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [12 April 2020 10:57 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/31 "2020-04-12T10:57:32Z")

</div>

Or use something like this instead of the Json parser node:

```auto
[{"id":"9f0f80da.d115c8","type":"function","z":"ed7ded5f.9a395","name":"JSON or String","func":"try{\n msg.payload = JSON.parse(msg.payload);\n return [msg, null];\n}\ncatch(error){\n return [null, msg];\n}","outputs":2,"noerr":0,"x":360,"y":580,"wires":[["2b701117.423716"],["9dbceb14.8e76b8"]]},{"id":"a235aa0e.32adb8","type":"inject","z":"ed7ded5f.9a395","name":"A JSON string","topic":"","payload":"{\"test\": \"i am a json\"}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":540,"wires":[["9f0f80da.d115c8"]]},{"id":"6577a3e.36d2bdc","type":"inject","z":"ed7ded5f.9a395","name":"A string string","topic":"","payload":"i am a string","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":620,"wires":[["9f0f80da.d115c8"]]},{"id":"2b701117.423716","type":"debug","z":"ed7ded5f.9a395","name":"JSON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":540,"wires":[]},{"id":"9dbceb14.8e76b8","type":"debug","z":"ed7ded5f.9a395","name":"String","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":620,"wires":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e64af91015e0c4812da68a024c57906c15ccabfe.jpeg)  
it’s a very simple function:

```auto
try{
    msg.payload = JSON.parse(msg.payload);
    return [msg, null];
}
catch(error){
    return [null, msg];
}

```

Johannes

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [13 April 2020 07:53 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/32 "2020-04-13T07:53:47Z")

</div>

Hi,

I changed the message format in the domoticz script code to a JSON format.  
Now I have the following incoming message (without JSON errors):

```auto
13-4-2020 09:41:49node: 728933bc.7037dc
domoticz/out : msg.payload : Object
object
bericht: "De frietpan is aangezet!"

```

Since I am a beginner and my node-red abilities are still under construction 😉 I have a question:  
I want to write a function node which does a string comparison.  
I tried this, but obviously the Debug node, does not return anything.

```auto
if (msg.payload.bericht == 'De frietpan is klaar om te frituren!') {
      
      node.send(msgFrietpanKlaar);

      setTimeout(function(){
        node.send(msgVolumeBack);
      }, 7000);
      
}

```

How do you compare a string value in javascript?

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [13 April 2020 08:17 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/33 "2020-04-13T08:17:14Z")

</div>

The string comparison looks fine. But it’s a different string than the one in the debug?

> [@gschmidt](#):
>
> bericht: "De frietpan is aangezet!"

> [@gschmidt](#):
>
> msg.payload.bericht == 'De frietpan is klaar om te frituren!'

?  
And I assume that the is not the whole function node. Where do you define the objects you use in the node.send() functions?

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [13 April 2020 08:34 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/34 "2020-04-13T08:34:34Z")

</div>

Oh sorry,

From the domoticz script actually 4 messages could be send:

```auto
local msgAan = 'De frietpan is aangezet!'
local msgKlaar = 'De frietpan is klaar om te frituren!' (send with a 15 minutes delay)
local msgWordt = 'De frietpan staat nog aan en wordt nu uitgezet!' (send with a 60 minutes delay)
local msgUit = 'De frietpan is uitgezet!'

```

But in the node-red flow I only want to respond on the second and the third message. (the message you saw was a test if the message correctly was incoming in JSON format.)

The idea is that when the Frietpan Switch is turned on I receive a message after 15 minutes that the 'De frietpan is klaar om te frituren!' and if the Frietpan switch is still on after 60 minutes, it will be turned off automatically. This is all working in domoticz fine. The messages above are send to Telegram, but I want to use the messages also in node-red for voice messaging to my google home devices.

The full flow looks like this:

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b6d4fce94f607884af67230a33158dda5b670d9.png)

The Function code like this:

```auto
var msgFrietpanKlaar = {
    "url": "http://192.168.1.51:1880/frietpan-klaar",
    "contentType": "audio/mp3",
    "volume": 60
}

var msgFrietpanNogAan = {
    "url": "http://192.168.1.51:1880/frietpan-nog-aan",
    "contentType": "audio/mp3",
    "volume": 60
}

var msgVolumeBack = {
    "volume": 30
}

if (msg.payload.bericht == 'De frietpan is klaar om te frituren!') {
      
      node.send(msgFrietpanKlaar);

      setTimeout(function(){
        node.send(msgVolumeBack);
      }, 7000);
      
}

if (msg.payload.bericht == 'De frietpan staat nog aan en wordt nu uitgezet!') {
      
      node.send(msgFrietpanNogAan);

      setTimeout(function(){
        node.send(msgVolumeBack);
      }, 7000);
      
}

```

but when I test the switch the node-red function node only returns: undefined

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [13 April 2020 08:45 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/35 "2020-04-13T08:45:54Z")

</div>

Have you switched your debug to show you the whole message object? You can do this in the menu of the debug node you attached to the function.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [13 April 2020 08:49 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/36 "2020-04-13T08:49:23Z")

</div>

You are not returning a msg.payload so if you have a debug node set to display msg.payload it will get an 'undefined'

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [13 April 2020 08:56 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/37 "2020-04-13T08:56:54Z")

</div>

Ah OK, after I set the debug node to "complete msg object" I see all messages in the debug.  
This seems to work corrrectly.

Do I have to change the function then to debug correctly to the msg.payload then? And if so how to do this?

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [13 April 2020 09:03 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/38 "2020-04-13T09:03:25Z")

</div>

There is no real need to move it to msg payload but it’s very easy to do.  
instead of `node.send(yourmsgobject)` use `node.send({payload:yourmsgobject})`  
Johannes

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 April 2020 10:02 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/39 "2020-04-13T10:02:56Z")

</div>

Rather than using complex stuff like setTimeout in a node you might be better to use a Switch node to route the different messages down different paths then Change nodes to setup the data and Trigger to send one message then after the interval to send the other.

---

<div class="post-metadata">

### Author: ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)
#### Post date: [13 April 2020 10:46 UTC](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680/40 "2020-04-13T10:46:08Z")

</div>

> [@JGKK](#):
>
> There is no real need to move it to msg payload but it’s very easy to do.  
> instead of `node.send(yourmsgobject)` use `node.send({payload:yourmsgobject})`  
> Johannes

Your right, I only want to see in the debug node the messages of the cast node, which are returned correctly now thanx!

[Previous page](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680.md?page=1)

[Next page](https://discourse.nodered.org/t/unexpected-token-d-in-json-at-position-0/24680.md?page=3)
