# How to add key/value of json object by node change

**URL:** <https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662>\
**Category:** General\
**Created:** [26 February 2021 05:19 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662 "2021-02-26T05:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 February 2021 05:19 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/1 "2021-02-26T05:19:54Z")

</div>

Hi,  
I would like to add key/value to the json object, I have done it using the `function` node.

![objJsonChangeNode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fa6d9b26bac127f8c5ae3765e0d9c38d6dfdf1df.png)

```auto
[{"id":"828012fb.ba609","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"b7825fef.5e89","type":"inject","z":"828012fb.ba609","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":260,"wires":[["d20aed86.d3124"]]},{"id":"864f5526.8351b8","type":"function","z":"828012fb.ba609","name":"get data","func":"var arrVal = [];\narrVal.push('111');\narrVal.push('222');\narrVal.push('333');\nmsg.arrVal = arrVal;\n\nmsg.key = 'Data.txt';\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":260,"wires":[["f517bb09.3554e8","d0c6dad4.d1bf48"]]},{"id":"d20aed86.d3124","type":"change","z":"828012fb.ba609","name":"","rules":[{"t":"set","p":"objFiles","pt":"msg","to":"{}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":260,"wires":[["864f5526.8351b8"]]},{"id":"f517bb09.3554e8","type":"function","z":"828012fb.ba609","name":"store data","func":"msg.objFiles[msg.key] = msg.arrVal;\nreturn msg;","outputs":1,"noerr":0,"x":860,"y":220,"wires":[["671a7728.0305e8"]]},{"id":"671a7728.0305e8","type":"debug","z":"828012fb.ba609","name":"objFiles","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"objFiles","targetType":"msg","x":1000,"y":220,"wires":[]},{"id":"d0c6dad4.d1bf48","type":"change","z":"828012fb.ba609","name":"store data","rules":[],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":320,"wires":[[]]}]

```

Is this possible using `change` node?  
Please tell me!  
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:** [26 February 2021 08:32 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/2 "2021-02-26T08:32:29Z")

</div>

Sure

```auto
[{"id":"d0c6dad4.d1bf48","type":"change","z":"828012fb.ba609","name":"store data","rules":[{"t":"set","p":"objFiles","pt":"msg","to":"{key: arrVal}\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":320,"wires":[["671a7728.0305e8"]]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 February 2021 09:11 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/3 "2021-02-26T09:11:23Z")

</div>

An important clarification to the solution provided by @E1cid is that it will _replace_ the whole `msg.objFiles` object with one containing just the one key/value pair.

In the context for the example flow you've provided, that is fine and also means you can get rid of the first Function node what initialises `msg.objFiles` to an empty object.

In the more general case, as you ask how to _add_ a key/value property to a message property, that currently requires a bit more complex JSONata expression:

```auto
$merge([$.objFiles,{$.key: $.arrVal}])

```

In the 1.3 release, you'd be able to do this without JSONata by setting the `msg.` type to: `msg.objFiles[msg.key]`

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

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 February 2021 19:31 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/4 "2021-02-26T19:31:28Z")

</div>

@E1cid, @knolleary Thank your for your support!

Great!!!  
Maybe jsonata can do it all @@

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [26 February 2021 20:28 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/5 "2021-02-26T20:28:27Z")

</div>

> [@knolleary](#):
>
> In the 1.3 release, you'd be able to do this without JSONata by setting the `msg.` type to: `msg.objFiles[msg.key]`

One useful information I will keep in mind.  
Thank you!

 ![node-red-version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5269ea3646b850aefaa056e73d53f7e7493cce2.png)

---

<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:** [12 March 2021 20:29 UTC](https://discourse.nodered.org/t/how-to-add-key-value-of-json-object-by-node-change/41662/6 "2021-03-12T20:29:08Z")

</div>

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