# Apples and Bananas but no Fruit

**URL:** https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875
**Category:** General
**Created:** [23 February 2022 11:19 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875 "2022-02-23T11:19:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![CarloJongen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/carlojongen/32/33683_2.png) [@CarloJongen](https://discourse.nodered.org/u/CarloJongen)
#### Post date: [23 February 2022 11:19 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/1 "2022-02-23T11:19:23Z")

</div>

So I've made this simple setup.  
I inject a Json msg "A" and this results in Global TEST "A". When I inject Json msg "B" I get the Global TEST "B" as result.  
But what if I only wanted to change the amount Apples & Bananas without changing the payload "Fruit".  
How would one change only one item in the TEST json msg without changing any of the other items? Can this be done directly?

 ![NR_Fruit_Example](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/3102eda76ad8da292a87e31bb1a42ea16a6abb6b.jpeg)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 February 2022 11:38 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/2 "2022-02-23T11:38:11Z")

</div>

I don't understand what you mean. Please select those three nodes, Export them, and Paste them here, using the `</>` button at the top of the forum entry window when pasting. Then your question may make more sense.

---

<div class="post-metadata">

### Author: ![CarloJongen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/carlojongen/32/33683_2.png) [@CarloJongen](https://discourse.nodered.org/u/CarloJongen)
#### Post date: [23 February 2022 11:42 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/3 "2022-02-23T11:42:36Z")

</div>

```auto
[
    {
        "id": "2cb8209b8102f7f2",
        "type": "inject",
        "z": "ada58d9a.2eab9",
        "name": "{\"payload\":\"fruit\",\"apples\":1,\"bananas\":2}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"payload\":\"fruit\",\"apples\":1,\"bananas\":2}",
        "payloadType": "json",
        "x": 2460,
        "y": 1500,
        "wires": [
            [
                "9a6bdd2aa2b28a0d"
            ]
        ]
    },
    {
        "id": "9a6bdd2aa2b28a0d",
        "type": "function",
        "z": "ada58d9a.2eab9",
        "name": "Set Payload to Global Set \"TEST\"",
        "func": "global.set(\"TEST\",msg.payload);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2840,
        "y": 1520,
        "wires": [
            []
        ]
    },
    {
        "id": "11a18762e4b937f5",
        "type": "inject",
        "z": "ada58d9a.2eab9",
        "name": "{\"apples\":4,\"bananas\":5}",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"apples\":4,\"bananas\":5}",
        "payloadType": "json",
        "x": 2410,
        "y": 1540,
        "wires": [
            [
                "9a6bdd2aa2b28a0d"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![CarloJongen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/carlojongen/32/33683_2.png) [@CarloJongen](https://discourse.nodered.org/u/CarloJongen)
#### Post date: [23 February 2022 11:51 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/4 "2022-02-23T11:51:03Z")

</div>

I sort of would like to do the same like a Dictionary in Python.  
I have several items in a dictionary and I want to change only one item of the dictionary without changing any of the other items.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 February 2022 11:53 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/5 "2022-02-23T11:53:05Z")

</div>

This should do what you want I think

```auto
// fetch previous object, default to empty object
let data = global.get("TEST") || {}
//merge old object into new
data = Object.assign(data, msg.payload);
// save it
global.set("TEST",data);
return msg;

```

---

<div class="post-metadata">

### Author: ![CarloJongen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/carlojongen/32/33683_2.png) [@CarloJongen](https://discourse.nodered.org/u/CarloJongen)
#### Post date: [23 February 2022 12:00 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/6 "2022-02-23T12:00:01Z")

</div>

> [@Colin](#):
>
> ```auto
> // fetch previous object, default to empty object
> let data = global.get("TEST") || {}
> //merge old object into new
> data = Object.assign(data, msg.payload);
> // save it
> global.set("TEST",data);
> return msg;
> 
> ```

Thanks Colin that is exactly what I needed.

---

<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: [9 March 2022 12:00 UTC](https://discourse.nodered.org/t/apples-and-bananas-but-no-fruit/58875/7 "2022-03-09T12:00:43Z")

</div>

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