Putting the value of a global variable inside a line of an object from inject

Inject node has
{
"metrics": [
{
"name": "testing/test1",
"value": 11
},
{
"name": "testing/test2",
"value": 12
}
]
}

I then use the JSON node to convert to a string
"{"metrics":[{"name":"testing/test1","value":11},{"name":"testing/test2","value":12}]}"

I have an array of a global variable "Globaltag"
[ "DegreeC", "DegreeF", "Motor1", "Start_PB", "Stop_PB" ]

Now I need to replace the name values such as "testing/test1" with the contents of the global variable Globaltag[0]

How do I specify this in a change node? or is there another way to do this? I try this
change msg.payload
search for testing/test1
replace with global."Global[0]"

result is "{"metrics":[{"name":"undefined","value":11}......

I hope for "{"metrics":[{"name":"DegreeC","value":11}

I can manually use a function node and concat like
var Mystring = '{ "metrics": [ {"name": "' + global.get("Globaltag[0]") + .......

This will eventually get fed to a SparkplugB publish node to go to the broker.

Thank you.

Do not pass through json node as you require it to be a object to alter it .
Then in a change node
Assuming the metrics array is in payload( as you do not specify)
set msg. payload.metrics[0].name
to value of global. Globaltag[0]

If you require it to be a JSON string you can pass it through the JSON node after you have adjusted the properties.

Thank you so very much. Works perfectly. Have a great Holiday season.

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