# Parse JSON payload from Influx DB Query

**URL:** https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635
**Category:** General
**Created:** [14 May 2021 09:44 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635 "2021-05-14T09:44:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![crazykong](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@crazykong](https://discourse.nodered.org/u/crazykong)
#### Post date: [14 May 2021 09:44 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/1 "2021-05-14T09:44:37Z")

</div>

Hello Guys,

Just starting out on my Node Red journey and trying to build an intelligent solar PV switch and data logger. Switch and data logger is working fine. I'm using Influx DB to log, on a Pi Hassio platform. But having a real problem with something I thought was basic.

I got a query running which polls mean energy generation over the last 20 mins from Influx. This is the string returned.

I'm trying to get the data component highlighted. The mean valuw.

On the debug node, I'm getting "undefined".

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

This is the logic in the function node.

var arr = msg.payload;

for (var i = 0; i \< arr.length; i++){  
var obj = arr[i];  
msg.payload = obj.mean;

}

return msg;

When posting this logic and the string in an online JS tool this code works and returns the value.

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

It is probably something very obvious, but I spent a lot of head scratching and got nowhere. It would be fab if you guys could take a look and maybe give me some pointers.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [14 May 2021 09:50 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/2 "2021-05-14T09:50:41Z")

</div>

your return is a string, you will need to pass it through a json node to convert to an object, then mean will be at msg.payload[0].mean.

[edit Or the node that generated the string output may have an option to output an object instead.]

---

<div class="post-metadata">

### Author: ![crazykong](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@crazykong](https://discourse.nodered.org/u/crazykong)
#### Post date: [14 May 2021 09:59 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/3 "2021-05-14T09:59:48Z")

</div>

Thanks for this E1cid.

I thought I was already passing it through a json node. This is my flow...

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

These are the JSON node properties...

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

I'll try outputting JSON node as an object, but assume function code will need to change.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [14 May 2021 10:04 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/4 "2021-05-14T10:04:32Z")

</div>

copy and paste what is going into the json node, and what is coming out, no images as the make it diffcult to copy info.

---

<div class="post-metadata">

### Author: ![crazykong](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@crazykong](https://discourse.nodered.org/u/crazykong)
#### Post date: [14 May 2021 10:11 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/5 "2021-05-14T10:11:26Z")

</div>

Sure, this is debug before JSON node...

array[1]  
0: object  
time: "2021-05-14T09:48:50.415Z"  
mean: 490.9759036144578

This is debug after JSON node

string[93]  
[  
{  
"time": "2021-05-14T09:48:50.415Z",  
"mean": 490.9759036144578  
}  
]

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [14 May 2021 10:18 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/6 "2021-05-14T10:18:19Z")

</div>

remove the json node it is converting the object to a string.

---

<div class="post-metadata">

### Author: ![crazykong](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@crazykong](https://discourse.nodered.org/u/crazykong)
#### Post date: [14 May 2021 10:31 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/7 "2021-05-14T10:31:12Z")

</div>

Thanks again, I've done that now. What would be the best function logic to get the mean value within the array? I've seen something like this used before, but it doesn't seem to work here.

p = msg.payload[0];  
node.log(typeof p);  
msg.payload = p.mean;

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [14 May 2021 10:32 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/8 "2021-05-14T10:32:29Z")

</div>

I would use

```auto
msg.payload = msg.payload[0].mean;
returm msg;

```

but that could also be done in a change node very simply with out calling a function node.

---

<div class="post-metadata">

### Author: ![crazykong](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@crazykong](https://discourse.nodered.org/u/crazykong)
#### Post date: [14 May 2021 10:37 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/9 "2021-05-14T10:37:04Z")

</div>

Hi E1cid, used a change node, and this has solved the problem. Many thanks for your guidance.

---

<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: [28 May 2021 10:37 UTC](https://discourse.nodered.org/t/parse-json-payload-from-influx-db-query/45635/10 "2021-05-28T10:37:28Z")

</div>

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