Extract dynamic json object to payload

Hi,
I am new to node-red and also to javascript.
I intend to extract a dynamic key/values from the data_label object and put it in msg.payload just like the picture attached.
How can I achieve this with the function node?
Regards
Untitled

is this specific to data_label? If so, you can utilise a change node with JSONata:
$sift($merge([$$.payload, $$.payload.data_label]), function($val, $key) {$key != "data_label"})

https://try.jsonata.org/IOXYLdsO

What this does is that it will merge msg.payload object with the inner msg.payload.data_label object, which will give you a combined object of

{
  "@timestamp": "2020-02-24T...",
  "data_label": {
    "DATA0": 6573,
    "DATA1": 65
  },
  "DATA0": 6573,
  "DATA1": 65
}

Then, it will call a $sift function on it to filter out the key in the object called data_label to get the item out. Anyone having a more simplistic solution, feel free to show how to do it better, I love improved queries :slight_smile:

1 Like

Also have a look at the node red docs on Working With Messages, that will give you further help.

Exactly what I needed!
Thank you my friend.
Regards

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