How to avoid merge subsequent node outputs?

I am calling the downloader.download_file service and right after I am calling the notify service as shown below:

My problem is that downloader.download_file outputs it is data as for example:

{
    "data": 
    {
         "subdir": "downloads",
         "filename": "2023-3.29-motion.mp4"
    }
}

and that gets merged with the notify service JSON output:

{
    "title": "New Ring Video available",
    "message": "A new video Ring motion video is available",
    "data": {
        "group": "HomeAssistantNotifications",
        "timeout": 30,
        "push": {
            "interruption-level": "passive"
        }
    }
}

ending up with:

{
    "title": "New Ring Video available",
    "message": "A new video Ring motion video is available",
    "data": {
        "group": "HomeAssistantNotifications",
        "timeout": 30,
        "push": {
            "interruption-level": "passive"
        },
        "subdir": "downloads",
        "filename": "2023-3.29-motion.mp4"
    }
}

and of course, that is making the node fail with the following error:

“Call-service error. extra keys not allowed @ data[‘subdir’]”

I believe that is how NodeRED works by passing payload along between nodes to have the information available but is there any way to avoid this? what could I add in between to avoid this behavior?

I suggest putting a change node between the two service call nodes.

I find the Change node a valuable tool (and probably the node I use the most). As well as the default Set mode, this can be switched to Delete or Move. I use the ‘delete’ mode to remove unwanted items from the message when there is either too much going on or stuff I don’t want to conflict with nodes further down the flow.

Use as a delete to delete msg.data, or if you want to keep the output data, to move msg.data to something like msg.olddata.

A more comprehensive approach is to rebuild the entire or parts of the message from scratch using Set and a JSON expression. For example, using ‘Set’ for msg ‘data’ and ‘JSON’ and

{}

sets msg.data to an empty object

Note that the ‘msg.’ fields don’t have to be the entire msg.data - you can delete just the msg.data.subdir bit. The only time working with sub-fields becomes an issue is with trying to ‘set’ something like msg.payload.subfield where msg.payload is currently not an object. In most situations msg.payload is a non-object, so in this case I use two ‘set’ lines in the change node. The first to set msg.payload to JSON {} which is an empty object (thus clearing the existing msg.payload, and creating a new one as an object) and then the second line to set msg.payload.subfield to the value I want.

1 Like

Thanks for this, it works like a charm! I was losing my head trying to find a way to remove unwanted data.

I removed the entire payload since I was not using anything in the notify service