Reading json parameter in custom node

Hi,
I’ve have a custom node that has a parameter that I’ve defined as type json. In the workflow that uses this node, I’ve created an expression that returns valid json.

My question is what is the correct way to read this json parameter item? I cannot seem to find a sample in the documentation, and what I’ve tried doesn’t seem to return anything that can be used.

I’ve tried the following where the ‘attachments’ parameter should be valid json. I am constructing it by using an expression that looks like the following, where the incoming item has a json property called attachments that is an array of objects

Expression in parameter of node

{"attachments": {{$json["attachments"]}} }

Attempt to access value of the parameter in custom node

const note = this.getNodeParameter('body', i) as string;
const attachmentParameter = this.getNodeParameter('attachments', i) as any;

Hi @IbnJubayr, sorry to hear you’re running into this behaviour. Can you elaborate on the “doesn’t seem to return anything that can be used” part? What value would attachmentParameter in your example have?

If you’d like an example, the ClickUp would expect JSON in the customFieldsJson field which is validated and read like so. The validateJSON function from this example can be found here.

Hey @MutedJam - thanks for the response. What I’m receiving inside the custom node is something that looks like a string but is not valid json:

{"attachments": [Object object], [Object object]}

This obviously fails to parse as json, and I cannot seem to figure out how to pass it correctly in the expression builder. The result in the expression builder looks correct (see attached screenshot)

Capture|541x462

Try the expression like this: {{JSON.stringify({ "attachments": $json["attachments"] })}}

Regarding validating the attachment within the node, do the following:

let attachmentParameter = this.getNodeParameter('attachments', i) as string;

try {
   //try to parse it
    attachmentParameter = JSON.parse(attachmentParameter)
} catch (exception) {
   throw new Error('Field attachment needs to be a valid JSON')
}

//at this point attachmentParameter should be a valid JSON object

1 Like

This solved it - feeling like a bit of an idiot now, but thank you!

Is this the only way to pass ‘json’ from an expression back to a custom node?