# MQTT Json to Influxdb (node function)

**URL:** <https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424>\
**Category:** General\
**Created:** [3 December 2018 00:53 UTC](https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424 "2018-12-03T00:53:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luisgcu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luisgcu/32/4549_2.png) [@luisgcu](https://discourse.nodered.org/u/luisgcu)\
**Post date:** [3 December 2018 00:53 UTC](https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424/1 "2018-12-03T00:53:02Z")

</div>

Hi guys,  
Need some help to improve function node that I have done to insert data into influxdb. data is coming out a Mqtt in json format.  
The topic have this structure : **/v1.6/devices/IOT\_ID2** where " **IOT\_ID2**" identify the device where the data is coming from.  
A sample payload from one device looks like the below:  
`{"PreRef8":2980,"PosRRef":2980,"CurrMag":20,"MotAccu":550,"MotRpm":894,"MotFrec":3163,"Analog1":9531,"InvTemp":61,"DcLinkV":328,"StatWrd":35,"Trip0":26,"EMeter":431,"ApR1825":-25,"Vinput":26,"PowerSta":0}"`

So decided to use the devices name as "measurement" requiered by influx, and all the variables included in the json would be the field.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/ff775cdae0ee9ebea03a1e9bc049c34aaae3a173.png)  
So I created the following function that I would like to improve a bit in order to get the **fields** automatically from the json pairs ? any ideas how to do that ?

```auto
var tokens = msg.topic.split("/");
msg.topic=tokens[3]; // get the device id from topic /v1.6/devices/device_id
var dest = tokens[tokens.length-1];
tmp=JSON.parse(msg.payload);
var key = Object.keys(msg.payload);

msg.payload = [
    {
        measurement: msg.topic, //Device id as topic
           fields: {
            PreRef8 :tmp.PreRef8,
            PosRRef :tmp.PosRRef,
            CurrMag :tmp.CurrMag,
            MotAccu :tmp.MotAccu,
            MotRpm :tmp.MotRpm,
            MotFrec :tmp.MotFrec,
            Analog1 :tmp.Analog1,
            InvTemp :tmp.InvTemp,
            DcLinkV :tmp.DcLinkV,
            StatWrd :tmp.StatWrd,
            Trip0 :tmp.Trip0,
            EMeter :tmp.EMeter,
            ApR1825 :tmp.ApR1825,
            Vinput :tmp.Vinput,
            PowerSta :tmp.PowerSta
        
        },
        timestamp: new Date() //insert time stamp
    },
    
];
return msg;  

```

that way I did it work s and this is the result below, but I would like to do it more sophisticated way , so i can feed any Json without writing manually the fields.  
Thanks in advanced for any hint.

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [3 December 2018 01:02 UTC](https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424/2 "2018-12-03T01:02:07Z")

</div>

You can simply do a for-in loop through the object.

> **[for...in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Examples)**
>
> The for...in statement iterates over all non-Symbol, enumerable properties of an object.

> <https://stackoverflow.com/questions/921789/how-to-loop-through-a-plain-javascript-object-with-the-objects-as-members>

---

<div class="post-metadata">

**Author:** ![luisgcu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luisgcu/32/4549_2.png) [@luisgcu](https://discourse.nodered.org/u/luisgcu)\
**Post date:** [4 December 2018 02:29 UTC](https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424/3 "2018-12-04T02:29:46Z")

</div>

> [@luisgcu](#):
>
> PreRef8 :tmp.PreRef8, PosRRef :tmp.PosRRef, CurrMag :tmp.CurrMag, MotAccu :tmp.MotAccu, MotRpm :tmp.MotRpm, MotFrec :tmp.MotFrec, Analog1 :tmp.Analog1, InvTemp :tmp.InvTemp, DcLinkV :tmp.DcLinkV, StatWrd :tmp.StatWrd, Trip0 :tmp.Trip0, EMeter :tmp.EMeter,

thanks @TotallyInformation I get help from a friend and this was the exact code that worked , just in case someone has similar need.

```auto
var tokens = msg.topic.split("/");
msg.topic =tokens[3]; //get device name from topic level 3 /v1.6/devices/tokens[3]
var dest = tokens[tokens.length-1];
inputjson =JSON.parse(msg.payload);

var _fields = {};
for(var item in inputjson){
    _fields[item] = inputjson[item];
}

msg.payload = [
    {
        measurement: msg.topic, //device name as measurement
        fields: _fields,
        timestamp: new Date()
    },
    ];
return msg;

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2018 07:20 UTC](https://discourse.nodered.org/t/mqtt-json-to-influxdb-node-function/5424/4 "2018-12-04T07:20:11Z")

</div>

Cool. Note that you don't need to send the timestamp, InfluxDB will do that for you.
