Create a JSON object with dynamic key values

Hi there,

I'm stuck on something:

I need to write data containing a measurement to an external database which require a specific JSON format which look like this:

    "point": {
        "UNIXTIME1": VALUE1,
        "UNIXTIME2": VALUE2,
        and so on

for example:

    "point": {
        "1514764800000": 128,
        "1514768400000": 9.6,
        "1514772000000": 20.3,
        "1514775600000": 15.4,
        "1514779200000": 17.6

So the key of the value is the current time which I can get with
var t = d.getTime(new Date());

Now how to create keys based on this time and assign to it a value which is contained in msg.payload into another key value "point" (which is static) ?
The first step is to generate the JSON object containing 1 value on each measurement. The second would be to generate the output JSON object every X measurement after X occurence.

English is not my main langugage, I hope you can understand :wink:



You do not give an example of input, but you can use a join node in manual mode set to key object and set msg.timestamp as the key and move msg.payload to msg.point. You can set it to output after x number of inputs or a timeout, or both as in example below.

[{"id":"6d3bf8afbd822396","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$round($random()*10,2)","payloadType":"jsonata","x":130,"y":160,"wires":[["fa46d50387426fa7"]]},{"id":"fa46d50387426fa7","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"point","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":220,"wires":[["e0c6264cc85e5d30"]]},{"id":"e0c6264cc85e5d30","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"point","propertyType":"msg","key":"timestamp","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"20","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":160,"wires":[["9f4daf1389f3644a"]]},{"id":"9f4daf1389f3644a","type":"debug","z":"b9860b4b9de8c8da","name":"debug 328","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":160,"wires":[]}]

[edit] No coding required.

Option 1: Use square brackets for dynamic keys.

const name = 123456789
const obj = {
  [name]: 987654321
console.log(obj) // { "123456789": 987654321 }

Option 2: Just add the keys using square brackets

const name = 123456789
const obj = { }
obj[name] = 987654321
obj["another prop"] = "hi"
1 Like

my current code is:

var result = [];

// find unix time
var t = new Date().getTime();
// data to write to DB
var src = msg.payload.temperature;

//assign unix time to key
var key = t;
var entry = {}
entry[key] = src


msg.payload = result;
return msg;

which give this result:

0: object
1693316583161: 25.51

But I don't need the array and the object should be named "point"

great thank you :wink:

I gave you the tools necessary - so that you could figure it out an learn something :person_shrugging:

So, if you want msg.payload to be an object with a child object named points you need to first create an object and add a points object

// make objects
const newPayload = {}
newPayload.points = {}

then to add a dynamically created key/value, use square brackets as stated in my first post

// make objects
const newPayload = {}
newPayload.points = {}

// get time/data
const time1 = new Date().getTime()
const data1 = msg.payload.temperature

// add new key/value
newPayload.points[time1] = data1

// return it
msg.payload = newPayload
return msg

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