Help

How to input workflow data into a JSON body through Script

Topic Labels: Automations Integrations
Solved
Jump to Solution
2075 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Manuel-Tanco
4 - Data Explorer
4 - Data Explorer

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());

 

 

1 Solution

Accepted Solutions
Ben_Young1
11 - Venus
11 - Venus

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
}

See Solution in Thread

3 Replies 3
Ben_Young1
11 - Venus
11 - Venus

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
11 - Venus
11 - Venus

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
}
Manuel-Tanco
4 - Data Explorer
4 - Data Explorer

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