# How to change value of a json object key without using function node

**URL:** <https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253>\
**Category:** General\
**Created:** [14 October 2020 16:42 UTC](https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253 "2020-10-14T16:42:28Z")\
**Posts on this page:** 4\
**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:** [14 October 2020 16:42 UTC](https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253/1 "2020-10-14T16:42:29Z")

</div>

Hi,  
I want to change the value `mydb` of a json object key `DbName` to another value without using function node with input as **object name** and **key name**.

 ![change-value-obj](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53f4b0e178180664682a39e621054acfd5ca5f80.png)

```auto
[{"id":"bcd513be.ed647","type":"function","z":"ee38a730.b7ca38","name":"","func":"\nmsg.data = {DataBase:{IpAddr:'127.0.0.1',Port:'5432',User:'admin',Pass:'Pass',DbName:'mydb'}}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":2460,"y":120,"wires":[["744bb926.51df58"]]},{"id":"d7647d4c.b6643","type":"inject","z":"ee38a730.b7ca38","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":2110,"y":120,"wires":[["f1403d50.d4057"]]},{"id":"965aebfc.4106d8","type":"debug","z":"ee38a730.b7ca38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2470,"y":200,"wires":[]},{"id":"f1403d50.d4057","type":"change","z":"ee38a730.b7ca38","name":"","rules":[{"t":"set","p":"objName","pt":"msg","to":"DataBase","tot":"str"},{"t":"set","p":"keyName","pt":"msg","to":"DbName","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2280,"y":120,"wires":[["bcd513be.ed647"]]},{"id":"744bb926.51df58","type":"change","z":"ee38a730.b7ca38","name":"","rules":[{"t":"set","p":"property","pt":"msg","to":"'data.' & msg.objName & '.' & msg.keyName","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2290,"y":200,"wires":[["965aebfc.4106d8"]]}]

```

I thought to use `change` node in conjunction with using [$lookup()](https://docs.jsonata.org/object-functions#lookup) function to get value and change, ... but I don't know how to do ...  
Any hint will be great.  
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:** [14 October 2020 20:24 UTC](https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253/2 "2020-10-14T20:24:14Z")

</div>

Seems it is only half possible, If anyone can tell us how to reference a json key with a variable

heres how to change DbName. This is probably easier in a function node.

```auto
[{"id":"2276949d.730e7c","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":100,"y":260,"wires":[["543a8019.34c178"]]},{"id":"543a8019.34c178","type":"function","z":"8d22ae29.7df6d","name":"","func":"\nmsg.data = {DataBase:{IpAddr:'127.0.0.1',Port:'5432',User:'admin',Pass:'Pass',DbName:'mydb'}}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":260,"wires":[["38639907.24b546"]]},{"id":"38639907.24b546","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"objName","pt":"msg","to":"DataBase","tot":"str"},{"t":"set","p":"keyName","pt":"msg","to":"DbName","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":260,"wires":[["53e7be35.ddc9e"]]},{"id":"ca18810d.3ed83","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":340,"wires":[]},{"id":"53e7be35.ddc9e","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"data","pt":"msg","to":"(\t$keyName := keyName;\tdata ~> | DataBase | {$keyName: 'test'} |\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":320,"wires":[["ca18810d.3ed83"]]}]

```

> **[Other Operators · JSONata](https://docs.jsonata.org/other-operators#-------transform)**
>
> \## \`&\` (Concatenation)

  
Edit/  
Here it is-

```auto
[{"id":"2276949d.730e7c","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":100,"y":260,"wires":[["543a8019.34c178"]]},{"id":"543a8019.34c178","type":"function","z":"8d22ae29.7df6d","name":"","func":"\nmsg.data = {DataBase:{IpAddr:'127.0.0.1',Port:'5432',User:'admin',Pass:'Pass',DbName:'mydb'}}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":260,"wires":[["38639907.24b546"]]},{"id":"38639907.24b546","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"objName","pt":"msg","to":"DataBase","tot":"str"},{"t":"set","p":"keyName","pt":"msg","to":"DbName","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":260,"wires":[["53e7be35.ddc9e"]]},{"id":"53e7be35.ddc9e","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"data","pt":"msg","to":"{objName:$merge([$eval(\"data.\" & objName),{keyName: \"test\"}])}\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":320,"wires":[["ca18810d.3ed83"]]},{"id":"ca18810d.3ed83","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":340,"wires":[]}]

```

---

<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:** [15 October 2020 01:26 UTC](https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253/3 "2020-10-15T01:26:01Z")

</div>

Yeahhhh, i tried with some patterns and the results are always correct.  
Many thanks for your assistance! 😳

---

<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:** [29 October 2020 01:26 UTC](https://discourse.nodered.org/t/how-to-change-value-of-a-json-object-key-without-using-function-node/34253/4 "2020-10-29T01:26:07Z")

</div>

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