Easiest way of replacing a item in JSON

So, i got a JSON string like this:

{ "Attribute1":"Test","Value1":"Testvalue" }

It got like 20 Attributes with corresponding values. The String is being loaded and saved from and to a file, which isn´t a problem, but how can i easily change 1 Value in the existing String, coming from another source like MQTT.

I tried it using the change node and Flow messages, which works but i guess there might be a more efficient way to do that, maybe with a function node?

I´ll have to use this like a hundred times so it would be great to have only a few nodes to be configured.

Thanks in advance
CrunkFX

First convert it from a string to a javascript object using a JSON node (or if it is coming from MQTT then configure the MQTT node to output a Parsed JSON object, so it will parse the JSON and convert to an onbject.
Then you can join that message with the other MQTT message using the pattern in the Joining Streams example in the cookbook. That will give you both payloads in one message so you can combine them as appropriate.
However, if you have to do this 100 times I wonder whether there may be a better way of organising it. Perhaps if you explained the broader problem you are trying to solve someone may come up with a better solution.

1 Like

In fact you may be able to use the Merged Object mode of the Join Node. It depends on the detail of what you want to do.

I'll give this a try later thanks. Then I'll be able to come up with a better way of designing the Sender.

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