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

**URL:** <https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104>\
**Category:** General\
**Created:** [22 November 2023 16:55 UTC](https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104 "2023-11-22T16:55:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sysmac](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@Sysmac](https://discourse.nodered.org/u/Sysmac)\
**Post date:** [22 November 2023 16:55 UTC](https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104/1 "2023-11-22T16:55:59Z")

</div>

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.

---

<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:** [22 November 2023 17:15 UTC](https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104/2 "2023-11-22T17:15:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sysmac](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@Sysmac](https://discourse.nodered.org/u/Sysmac)\
**Post date:** [22 November 2023 18:33 UTC](https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104/3 "2023-11-22T18:33:02Z")

</div>

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

---

<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:** [6 December 2023 18:33 UTC](https://discourse.nodered.org/t/putting-the-value-of-a-global-variable-inside-a-line-of-an-object-from-inject/83104/4 "2023-12-06T18:33:34Z")

</div>

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