# Error parsing JSON to influxDB

**URL:** <https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895>\
**Category:** General\
**Created:** [7 October 2020 15:38 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895 "2020-10-07T15:38:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [7 October 2020 15:38 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/1 "2020-10-07T15:38:35Z")

</div>

Hi,

I have the following flow

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14e753edc425281fc5afea88720e91f59b550b4d.png)

v2x=JSON.parse(msg.payload);  
node.log(typeof v2x);  
msg.payload={  
timestamp:v2x["timestamp"],  
latitude: parseFloat((v2x["message"]["basic\_container"]["reference\_position"]["latitude"])/10000000),  
longitude:parseFloat((v2x["message"]["basic\_container"]["reference\_position"]["longitude"])/10000000),  
heading:v2x["message"]["high\_frequency\_container"]["heading"],  
speed:v2x["message"]["high\_frequency\_container"]["speed"],  
curvature:v2x["message"]["high\_frequency\_container"]["curvature"],  
long\_acc:v2x["message"]["high\_frequency\_container"]["long\_acc"],  
yaw\_rate:v2x["message"]["high\_frequency\_container"]["yaw\_rate"],  
denm\_lat:parseFloat((v2x["message"]["management\_container"]["event\_position"]["latitude"])/10000000),  
denm\_long:parseFloat((v2x["message"]["management\_container"]["event\_position"]["longitude"])/10000000)};  
return msg;

I get the following error:

"TypeError: Cannot read property 'reference\_position' of undefined"

I get the same error in "event\_position"

I've installed the toFloat plugin.

Thanks

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [7 October 2020 16:13 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/2 "2020-10-07T16:13:31Z")

</div>

> of undefined

which mean it does not exist in the incoming payload. First check the incoming messages.

---

<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:** [7 October 2020 16:21 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/3 "2020-10-07T16:21:37Z")

</div>

If you can't work it out, add a debug node and show us the messages from both mqtt nodes.

---

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [7 October 2020 18:28 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/4 "2020-10-07T18:28:11Z")

</div>

Here the JSON files:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16dad0c5d5b759c758c36e7fea2c06a9887eb829.png)

Those fields exist.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 October 2020 18:36 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/5 "2020-10-07T18:36:45Z")

</div>

The second doesn’t seem to have a basic\_container

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [7 October 2020 21:14 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/6 "2020-10-07T21:14:14Z")

</div>

Hi,

i think the problem with your flow is that you make the assumption that your two mqtt messages will arrive at the function simultaneously and that all their values will be available.  
Just because they are wired to the same function doesnt mean that their values will both be accessible at that specific time. They arrive at different times so one or the other set of values will be undefined.

A way to solve this would be to use a Join node to wait for the two mqtt messages and then send the merged mqtt message to the function

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab452d854f6fc7f71160eb600f613e1b566a2021.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d4e24a4d020919b92c621d53cd228d4deb0328b.png)

Using a complete msg debug node after the Join node will make the new msg structure obvious and easier to make the necessary changes in your function.

Also if you are using Json String as an Mqtt message then you could convert it into a JS Object straight in the MQTT nodes so the will be no need to do `JSON.parse()` in the function

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49730758a33d399a2a1485737652c3eb3f8dc1fe.png)

---

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [8 October 2020 09:47 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/7 "2020-10-08T09:47:31Z")

</div>

Hi @UnborN, I've created the following flow as you suggested:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a1cf18e1c641b03feb6bbc4279e79b6a3187e731.png)

However, I only receive information directly from MQTT, the rest of configuration is:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aa6a08120f0edb78b3c62586d728385925cdf5da.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53cb6767493b6d60b6cbcebc0bf9142034b443e7.png)

This inside function:

node.log(typeof v2x);  
msg.payload={  
timestamp:v2x["timestamp"],  
latitude: parseFloat((v2x["message"]["basic\_container"]["reference\_position"]["latitude"])/10000000),  
longitude:parseFloat((v2x["message"]["basic\_container"]["reference\_position"]["longitude"])/10000000),  
heading:v2x["message"]["high\_frequency\_container"]["heading"],  
speed:v2x["message"]["high\_frequency\_container"]["speed"],  
curvature:v2x["message"]["high\_frequency\_container"]["curvature"],  
long\_acc:v2x["message"]["high\_frequency\_container"]["long\_acc"],  
yaw\_rate:v2x["message"]["high\_frequency\_container"]["yaw\_rate"],  
denm\_lat:parseFloat((v2x["message"]["management\_container"]["event\_position"]["latitude"])/10000000),  
denm\_long:parseFloat((v2x["message"]["management\_container"]["event\_position"]["longitude"])/10000000)};  
return msg;

I don't get information from the other debug nodes.

Thanks

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [8 October 2020 09:59 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/8 "2020-10-08T09:59:03Z")

</div>

You missed an important part of the **Join node** configuration  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/69aee6cedb72b3185b6a7284bb07ccd6fb7e8c7f.png)

Which tells the Join node to wait for the 2 seperate mqtt messages before joining them and sending them further down the line (to the function)

---

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [8 October 2020 10:43 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/9 "2020-10-08T10:43:21Z")

</div>

@UnborN, yes, I forgot to check that

I still have an error with the parsing

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3287a01eef4e19da2e127500f8f0e83a283cdcb.png)

It says that cannot read basic\_container, however it is in the left JSON structure, is it still getting the left parameters?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [8 October 2020 10:48 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/10 "2020-10-08T10:48:13Z")

</div>

> [@rparada](#):
>
> is it still getting the left parameters?

can you send us a new debug screenshot of the result you get after the Join node ? (debug named "1")  
expand the msg to see the full structure of the message. (the important parts)

---

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [8 October 2020 10:53 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/11 "2020-10-08T10:53:01Z")

</div>

@UnborN, find it below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/787973cce33d0b51897d4950145b5f382f7d0814.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 October 2020 11:08 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/12 "2020-10-08T11:08:02Z")

</div>

`basic_container` lives in `rx/cam/message/basic_container` i am not sure that a slash is valid in a property name, but you can extract it like:

```auto
msg.payload['rx/cam'].message.basic_container

```

eg.

```auto
latitude: parseFloat((msg.payload['rx/cam'].message.basic_container.reference_position.latitude)/10000000)

```

---

<div class="post-metadata">

**Author:** ![rparada](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rparada](https://discourse.nodered.org/u/rparada)\
**Post date:** [8 October 2020 11:29 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/13 "2020-10-08T11:29:29Z")

</div>

I modified the function to:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b1f4f296f2f09c75be4820824247c87a6317c48.png)

In debug2, the output is correct, however the influxDB provokes this error:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/0747c155de0f359cc5fbf39bd7b690acbc4d6cdc.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [8 October 2020 12:09 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/14 "2020-10-08T12:09:41Z")

</div>

I notice that **long\_acc** is still undefined .. i dont know much about influxDb and how it handles undefined values. If you expand Debug 2 you can check if anything is missing or undefined.

Another thing you may want to test is .. i saw that during the tests and experimentations to get the right msg.payload to the influxDb out node .. you had it wired to the rest of the flow and a wrong Measurement 'table' may have been created in you db.

As a test can you create a new Measurement/table (to start clean) .. or Drop the existing one  
**IF no important data are in the db !!!**

> **[Manage your database using InfluxQL | InfluxDB v1.8 Documentation](https://docs.influxdata.com/influxdb/v1.8/query_language/manage-database/#delete-measurements-with-drop-measurement)**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 October 2020 12:09 UTC](https://discourse.nodered.org/t/error-parsing-json-to-influxdb/33895/15 "2020-10-22T12:09:43Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
