Insert variable into JSON

Good Afternoon,

Can someone tell me how to insert my noted Hubspot properties into the code shown? I expect this is not the best way to do this, but I’m just limping through getting this to work. This code currently will prefill and submit a MS form from a custom coded HS workflow.

Right now the underlined values are hard coded and I’d like them to be HS properties.

I’ve googled and found a variety of answers, maybe something like adding +kone_po_chanages+ and possibly needing a “\” or double curly brackets. I just can’t find the right combination of characters.

so …\“answer1”: MYVARIABLE HERE}

Hi @SMCNULTY,

In order to insert the properties in your code, you will need to pass in an event parameter to your function:

function main(event) {

const kone_po_chanages = event.inputFields['kone_po_chanages'];

// rest of your code

}

Then you can use backticks to build your answers variable, which will allow to reference the properties, i.e.:

const answersString = “[{“questionid”:“r515917882df414883dfa9c75a732c9”, answer:”${kone_po_chanages}"}];

Which you can then use to build your data string:

let data - JSON.stringify({
 "startDate": "2024-12-1722: 06:06.493Z",
 "submitDate": "2024-12-17T22:06:36.8942",
 "answers" : answersString,
 "emailReceiptConsent": false
});

Thank you for this response. I think I’m close here, but I noticed in your code examples you removed the back slashes. Should I be doing the same? This is giving me error “7:112 PARSING ERROR - UNTERMINATED TEMPLATE”

I tried this, to mirror the other working parts but still getting the error:

Yes, when using backticks to wrap your string you don’t need to use backslashes to escape any characters.

Also, I see two additional issues in your latest screenshot:

1) the “asnwersString” is missing the closing backtick

2) your function is still missing the “event” paramenter

OK so now closer still. I’m executing this code in HS and it’s showing as successful, no errors, I’m also using a test MS form and I’m seeing the form submitted, but the values in the HS properties are not pushing over. The values were pushing over previously when I had the values hard coded. (not referencing variables/properties)

This is the spreadsheet that populates each time a form is submitted. row 30/31 were submissions from the HS custom code and row 32 is a test push from postman. (Postman has the values hard coded)

I also changed answer to answer1 (because that’s how it was in my working code) and I removed the reference and just put back in a hard value and those are not transfering either.

Here is Postman code that works:

Looks like the code I provided initially did not have the “answer” wrapped in double quotes. My bad.

const answerString = [{“questionid”: “r515917882df414883dfa9c75a732c9”, “answer”:“${p_o_}”), {“questionid”:“rb7515a5f9de649d0a831a0ec26afa9b5”, “answer”:“${kone_po_changes)”}];

Also, just to double check, you can add a few console logs to see if the values are actually showing up before getting sent.

console.log("Kone po: " + kone_po_changes);
console.log("PO: " + p_o_);

OK well I’m fried for the day. I’m calling it. I alternately tried both line 9 and line 10. Both still submitted blank forms. For kicks I changed answer to answer1 (as shown in Postman) and the form doesn’t even submit when I do that. So I still can’t push any value over, hardcoded or HS property.

I’ll try some logs in the AM unless you see something wrong

Hard to say without seeing the full code but there are a few things I notice that could be the issue.

One of them is that we’re passing the answers as a string instead of an array and the others could be potential “typos” in the labels and the values from copy/pasting.

Try replacing the “data” variable with this format. I intentionally made question ids be XXXXXXXX so that you could paste it from the source (instead of this thread)

let data = JSON.stringify({
 startDate: "2024-12-18T16:43:22.832Z",
 submitDate: "2024-12-18T16:43:33.845Z",
 answers: [
 {
 questionId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 
 answer1: kone_po_changes
 },
 {
 questionId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 answer1: p_o_
 }
 ]
});

OK I finally got it going. There were two problems. One of the question ID’s was wrong and it turns out I did need “answer1” and not just “answer”. So you were right, both typos in labels and an issue when I cut/paste the ID’s.

I was able to get this to work with both line 9 and line 10.

Thanks so much for your help! I absolutely never would have figured this out on my own.

Awesome! Glad to hear it worked :slightly_smiling_face: