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?
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.
[
{
"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"
]
]
}
]
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.
This should do what you want I think
// 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.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.