Skip to main content
Solved

How to input workflow data into a JSON body through Script


Forum|alt.badge.img+1

So, with my limited scripting and Java skills I've been trying to set up a script that will call a webhook so I can later trigger something there. I managed to set up the basic ping but I came across the ideal of sending some record ID's I need which can save me a step later on.

My error is surrounding inputConfig and how to parse it into the JSON body (I guess?)

The error most likely lies in line 4 and 5, hopefully it's understandable what I'm trying to get done. For line 4 and 5 I also tried "" but that simply sends over the whole thing and not the input I setup in the script (picture attached below)

 

let inputConfig = input.config(); let body = { "ping": "Survey Ping", "company_id":${inputConfig.contact_id}, "contact_id":${inputConfig.company_id} } let response = await fetch('https://n8n.joinowners.co/webhook-test/tally-onboardinglaunch-survey', { method: 'POST', body: JSON.stringify(body), headers: { "Content-Type": "application/json", "Accept": "application/json", }, }); console.log(await response.json());

 

 

Best answer by Ben_Young1

As some additional insight into why your original script wasn't working, you were trying to insert the values of inputConfig.contact_id and inputConfig.company_id using template literals, but you forgot to properly enclose the values inside of backticks. So you'd switch it from this:

let body = { "ping": "Survey Ping", "company_id": ${inputConfig.contact_id}, "contact_id": ${inputConfig.company_id} }

To this:

let body = { "ping": "Survey Ping", "company_id": `${inputConfig.contact_id}`, "contact_id": `${inputConfig.company_id}` }

You actually don't have to do this at all. You can actually just simply invoke the properties without enclosing them inside of the template literals like this:

 

let body = { "ping": "Survey Ping", "company_id": inputConfig.contact_id, "contact_id": inputConfig.company_id }
View original
Did this topic help you find an answer to your question?

3 replies

Ben_Young1
Forum|alt.badge.img+20
  • Brainy
  • 520 replies
  • March 28, 2023

Hey @Manuel-Tanco

Give this a shot:

 

const config = input.config(); const { company_id, contact_id } = config; const body = { ping: "Survey Ping", company_id: company_id, contact_id: contact_id }; const url = "https://n8n.joinowners.co/webhook-test/tally-onboardinglaunch-survey"; const options = { method: "POST", headers: { "Content-Type": "application/json", "Accept": "application/json" }, body: JSON.stringify(body) }; let data = await fetch(url, options) .then(response => { console.log(response); return response.json(); }); console.log(data)

 


Ben_Young1
Forum|alt.badge.img+20
  • Brainy
  • 520 replies
  • Answer
  • March 28, 2023

As some additional insight into why your original script wasn't working, you were trying to insert the values of inputConfig.contact_id and inputConfig.company_id using template literals, but you forgot to properly enclose the values inside of backticks. So you'd switch it from this:

let body = { "ping": "Survey Ping", "company_id": ${inputConfig.contact_id}, "contact_id": ${inputConfig.company_id} }

To this:

let body = { "ping": "Survey Ping", "company_id": `${inputConfig.contact_id}`, "contact_id": `${inputConfig.company_id}` }

You actually don't have to do this at all. You can actually just simply invoke the properties without enclosing them inside of the template literals like this:

 

let body = { "ping": "Survey Ping", "company_id": inputConfig.contact_id, "contact_id": inputConfig.company_id }

Forum|alt.badge.img+1
  • Author
  • New Participant
  • 1 reply
  • March 28, 2023

Okay understood, I was having a hard time trying to get synced with the correct way of writing it down. Thank you @Ben_Young1 !


Reply