How to change this object

I have this


but I want object like this:
t10001 : -32767
t10002 : 0
ect

You can do it in a function like this...

var data = msg.payload.form.text;
var result = {};
for(let i = 0; i < data.length; i++) {
    let key = data[i].id[0];
    let val = data[i].value[0];
    result[key] = val
}
msg.payload = result;
return msg;

Demo flow...

[{"id":"aa6ae4c8.748108","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"form\":{\"text\":[{\"id\":[\"t10001\"],\"value\":[\"-32767\"]},{\"id\":[\"t10002\"],\"value\":[\"32000\"]}]}}","payloadType":"json","x":690,"y":2600,"wires":[["ee08357c.961768"]]},{"id":"9414e97e.7f7078","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":2600,"wires":[]},{"id":"ee08357c.961768","type":"function","z":"4b3f21a3.ba434","name":"","func":"var data = msg.payload.form.text;\nvar result = {};\nfor(let i = 0; i < data.length; i++) {\n    let key = data[i].id[0];\n    let val = data[i].value[0];\n    result[key] = val\n}\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":2600,"wires":[["9414e97e.7f7078"]]}]
1 Like

You can do this in a change node using JSONata expression

payload.form.text.${id:value}

e.g.

[{"id":"8c8ae4ca.852eb","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"form\":{\"text\":[{\"id\":\"1\",\"value\":10},{\"id\":\"2\",\"value\":20}]}}","payloadType":"json","x":190,"y":4300,"wires":[["db9adee9.06f1e8"]]},{"id":"db9adee9.06f1e8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.form.text.${id:value}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":4380,"wires":[["6220f2d8.c1e9dc"]]},{"id":"6220f2d8.c1e9dc","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":4200,"wires":[]}]

Invalid JSONata expression: Key in object structure must evaluate to a string; got: ["t10001"]
obraz

value = " "

Please supply an example of the incoming payload that can be copied, If this fails.

It looks like you values are in arrays
try

payload.form.text.${id[0]:value[0]}

I think, problem is witch char ""

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