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

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.

[{"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() function to get value and change, ... but I don't know how to do ...
Any hint will be great.
Thanks.

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.

[{"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"]]}]

Edit/
Here it is-
[{"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":[]}]
1 Like

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

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