# Problem to replace a JSON object in Array

**URL:** https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658
**Category:** General
**Created:** [6 November 2023 16:21 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658 "2023-11-06T16:21:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [6 November 2023 16:21 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/1 "2023-11-06T16:21:47Z")

</div>

Hi , I have a question which might be more related to JS than Node Red.....

I'm trying to replace a JSON object in a Array but can't get it work. Each time the new data replaced in the array is a string, but not an object. I tried different ways, but no luck.

However, when I set the same object to a new object in the same array, then it works perfectly....

Here is a snippest of the code I use to manipulate data before sending it back into the array :

```auto
var csv = global.get("csvContent");

// When device name info provided from DB : 
if (msg.topic == "DB_Device_Name" && flow.get("DeviceType") != "Not Set !") {
    for (let i = 0; i < csv.length; i++) {
      
        // Build final end point name :

        var descriptorDesc = csv[i].descriptor.desc;
        var descriptorLabel = csv[i].descriptor.label; //>> {desc:,label:01C01-VGW1_CH07_RxA04}
        node.warn("descriptorLabel " + descriptorLabel)

        descriptorLabel = msg.payload + "_CH0" + csv[i].chNumber + csv[i].rxTx + csv[i].vertexType + csv[i].audCh;

       let newDesc = {"desc" : descriptorDesc}
       let newLabel = {"label" : descriptorLabel}
        csv[i].descriptor = Object.assign({}, newDesc, newLabel )

        csv[i].test = Object.assign({}, newDesc, newLabel )

    }

global.set("csvContent", csv);

```

When I want to replace descriptor here is the result ( I also put some screenshots, as the code section does not show any difference) :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f44d9ceed657e399d4ae74eb00d86720202b23e3.png)

```auto
{"desc":"","label":"12345_CH07_RxA04"}

```

when I set it to csv[i].test :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3399a865927aaa1dbda6b6cd7e817e3a6be02fc.png)

```auto
{"desc":"","label":"12345_CH07_RxA04"}

```

If useful, here is also a copy of one of the objects from csvContent source ( and dest) global variable :

```auto
{"#":"device73.484735f6-a1ea-596e-8282-0cd1108e6be2.R01a52f67-554c-5791-8503-fcbee07f93d9","active":"true","bidirPartnerId":"null","codecFormat":"Audio","configPriority":"off","control":"off","custom":"{}","descriptor":"{\"desc\":\"\",\"label\":\"12345_CH07_RxA04\"}","extraAlertFilters":"[]","Factory label":"receiver_s06_a03","isIgmpSource":"false","mainDstIp":"null","mainDstMac":"null","mainDstPort":"null","mainDstVlan":"null","mainSrcGateway":"null","mainSrcIp":"null","mainSrcMac":"null","mainSrcNetmask":"null","public":"false","sdpSupport":"true","sipsMode":"NONE","spareDstIp":"null","spareDstMac":"null","spareDstPort":"null","spareDstVlan":"null","spareSrcGateway":"null","spareSrcIp":"null","spareSrcMac":"null","spareSrcNetmask":"null","tags":"[]","useAsEndpoint":"false","chNumber":7,"vertexType":"A","rxTx":"_Rx","audCh":"04","test":{"desc":"","label":"12345_CH07_RxA04"}}

```

Thanks to you in advance for your tips !

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 November 2023 16:36 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/2 "2023-11-06T16:36:14Z")

</div>

Not certain why that would be happening without adding some debugging etc. However, I'd likely side step the issue and simply assign a new object explicitly:

```auto
        csv[i].descriptor = {
           "desc" : descriptorDesc,
           "label" : descriptorLabel
        }

```

Note, you may have something affecting the context data by reference in another part of your flow (that's my disclaimer)

---

<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: [6 November 2023 17:12 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/3 "2023-11-06T17:12:39Z")

</div>

In CsvContent the property descriptor is a string, so you can not assign a object property to a string. Unless as in @Steve-Mcl suggestion you assign descriptor as an object.

---

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [6 November 2023 19:24 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/4 "2023-11-06T19:24:11Z")

</div>

Mmm sorry I sent a copy of the CSV after it was modified by the non-working script...

I can definitely say it's an object at start, otherwise the script would not even be able to build the csv[i].test object....

I think I checked already the other nodes in the flow, but @Steve-Mcl might be right, there might something which also brings modification to the problematic obj in the array.... I can't see any other reason right now.

---

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [7 November 2023 06:57 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/5 "2023-11-07T06:57:55Z")

</div>

Hi, I found where my problem comes from, but still can't explain it.

Downstream in the flow after setting up the descriptor value as an object, I have a node which stringify it, only to be displayed correctly in a table :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34a542ede0b3a24c2f0da2e022d6fcf6f0211520.png)

The code of this second node is :

```auto
// Manage datas for proper display in table : 
var csv = global.get("csvContent");

    for (let i = 0; i < csv.length; i++) {

        csv[i].descriptor = JSON.stringify(csv[i].descriptor);
    }

msg.payload = "setdata"

msg.data = csv;

return msg;

```

As you can see the output of this node right now is not connected anywhere, and I don't make use of global.set() to write back the array..... but the global stored array is being modified.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/473ba47586470c2d98f87793220a4a8ee7a77bb9.png)

As soon as I remove the link to this node, everything is fine :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/72bb0dd1a60cba94a56128fe45ec6caef7ed9a08.png)

am I missing something obvious ?

Thanks

---

<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: [7 November 2023 07:26 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/6 "2023-11-07T07:26:43Z")

</div>

Object are stored in one memory slot, and are passed by reference.  
So `csv = global.get("csvContent")` Tell js to reference the same memory. When you change csv the global is changed to as they reference the same memory  
try  
`csv = RED.util.cloneMessage(global.get("csvContent"))`

[https://nodered.org/docs/api/modules/v/0.20.0/@node-red\_util\_util.html#.cloneMessage](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html#.cloneMessage)

> **[Object references and copying](https://javascript.info/object-copy)**

---

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [7 November 2023 07:38 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/7 "2023-11-07T07:38:58Z")

</div>

ok thank you, it works.

I'll need to have a serious look on all this. My (wrong) guess was that a variable stored in any context could not be modified until we use any kind of context.set() to write it back....

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [7 November 2023 08:12 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/8 "2023-11-07T08:12:51Z")

</div>

> [@JeromeVISUALS](#):
>
> My (wrong) guess was that a variable stored in any context could not be modified until we use any kind of context.set()

For primitive types, that would be correct.

- strings
- integers
- booleans
- etc etc

But for complex types

- Arrays
- Objects
- classes
- etc etc

They are `pass-by-ref`

It's not Node RED behaving this way, more how javascript works

**EDIT**  
Oh - just seen the link provided by @E1cid that explains.

---

<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 January 2024 08:12 UTC](https://discourse.nodered.org/t/problem-to-replace-a-json-object-in-array/82658/9 "2024-01-06T08:12:55Z")

</div>

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