# IoT Hub Microsoft Azure Syntax

**URL:** <https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121>\
**Category:** General\
**Created:** [27 June 2018 13:45 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121 "2018-06-27T13:45:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [27 June 2018 13:45 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/1 "2018-06-27T13:45:32Z")

</div>

I am trying to get my data into my IoT Hub on Azure from a Modbus signal.

My flow is as follows:

`[{"id":"a40d774a.bf8648","type":"azureiothub","z":"53f246d7.f619f8","name":"Azure IoT Hub","protocol":"mqtt","x":780,"y":120,"wires":[[]]},{"id":"99bdb067.08498","type":"function","z":"53f246d7.f619f8","name":"","func":"msg.payload = msg.payload.split(\",\");\nmsg1 = '{\"deviceID\": \"WaveBuoy\", '\nmsg1 = msg1 + '\"SAK\": \"PrimaryKey=\", ' \nmsg1 = msg1 + '\"Protocol\": \"mqtt\", '\nmsg1 = msg1 + '\"Data\": { \"' + msg.topic + '\": \"' + msg.payload[4] + '\": \"' + msg.payload[5] + '\": \"'+ msg.payload[6] + '\"}}'\n\nnewMsg = { payload: msg1 };\nreturn newMsg;\n\nmsg1 = msg1 + '\"Data\": { \"' + msg.topic + '\": \"' + msg.payload + '\"}}'","outputs":1,"noerr":0,"x":370,"y":280,"wires":[["4073cbfd.cf8d14","a40d774a.bf8648"]]},{"id":"beb2fc2f.9da3b","type":"tcp in","z":"53f246d7.f619f8","name":"","server":"client","host":"xxx","port":"xxx","datamode":"stream","datatype":"utf8","newline":"","topic":"WaveBuoy","base64":false,"x":150,"y":280,"wires":[["99bdb067.08498"]]}]`

I am getting the following error:

msg : error  
`"SyntaxError: Unexpected token : in JSON at position 129` or `128`

My string looks like this:

`"{"deviceID": "WaveBuoy", "SAK": "PrimaryKey=", "Protocol": "mqtt", "Data": { "WaveBuoy": "0.01": "0.02": "0.09"}}"`

If anyone could aid me it would be very much appreciated.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 June 2018 13:48 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/2 "2018-06-27T13:48:40Z")

</div>

The problem is exactly what it says - at character 129 of your string there is an unexpected `:`.  
Reformatting it slightly will make it clearer:

```auto
{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "WaveBuoy": "0.01": "0.02": "0.09"
   }
}

```

The `WaveBuoy` property is not valid JSON. Should it be an array of values? Or should `0.02` and `0.09` be separate properties with they own key name?

---

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [27 June 2018 14:04 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/3 "2018-06-27T14:04:35Z")

</div>

The latter, separate properties, they represent vertical, northings and eastings.

I see, I think. How does one put as many variable in the data bit as I would like?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 June 2018 14:07 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/4 "2018-06-27T14:07:34Z")

</div>

Do you mean something like:

```auto
{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "WaveBuoy": {
         "vertical": "0.01",
         "northings": "0.02",
         "eastings": "0.09"
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [27 June 2018 14:09 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/5 "2018-06-27T14:09:08Z")

</div>

Exactly what I mean! Apologies if I was unclear. Or even this.

```auto
{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
         "vertical": "0.01",
         "northings": "0.02",
         "eastings": "0.09"
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 June 2018 14:18 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/6 "2018-06-27T14:18:39Z")

</div>

Currently you are manually building up the JSON string. That is quite an error-prone way of doing it. You can build up the equivalent JavaScript object and convert it to JSON at the end of the process.

```auto
var parts = msg.payload.split(",");

msg.payload = {
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "vertical": msg.payload[4],
      "northings": msg.payload[5] 
      "eastings": msg.payload[6] 
   }
};

msg.payload = JSON.stringify(msg.payload);

return msg;

```

You _could_ leave out the `JSON.stringify` line and then wire this node to a `JSON` node to do the conversion.

Nick

```auto

```

---

<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:** [27 June 2018 14:45 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/7 "2018-06-27T14:45:34Z")

</div>

In the interests of saving wear on the fingers you don’t need quotes round the keys unless they contain special characters or are otherwise capable of misinterpretation. So you could have

```auto
msg.payload = {
   deviceID: "WaveBuoy",
   SAK: "PrimaryKey=",
   Protocol: "mqtt",
   Data: {
      vertical: msg.payload[4],
      northings: msg.payload[5],
      eastings: msg.payload[6] 
   }
};

```

By the way there is a missing comma on the end of the northings line.

---

<div class="post-metadata">

**Author:** ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)\
**Post date:** [28 June 2018 07:28 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/8 "2018-06-28T07:28:25Z")

</div>

I have carried out your correction, along with the changes that you stated in my other thread. However It still doesn’t seem to work…

Have you transmitted data to an IoT Hub on Azure?

---

<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:** [28 June 2018 07:57 UTC](https://discourse.nodered.org/t/iot-hub-microsoft-azure-syntax/1121/9 "2018-06-28T07:57:19Z")

</div>

Oh, did I post on a different thread? I can’t find that one now.

No I have not used the node, I was just comparing what you have with the readme for the node. Post what you have in the function node now and show us what the output looks like in a debug node. Check what you see with the readme for the node.
