# Insert variable into JSON

**URL:** <https://community.hubspot.com/t/insert-variable-into-json/122755>\
**Category:** APIs & Integrations\
**Tags:** workflows\
**Created:** [December 18, 2024, 5:19pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755 "2024-12-18T17:19:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SMCNULTY](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@SMCNULTY](https://community.hubspot.com/u/SMCNULTY)\
**Post date:** [December 18, 2024, 5:19pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/1 "2024-12-18T17:19:39Z")

</div>

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}

 ![SMCNULTY_0-1734542015198.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/2/d23579e8f7b707217edea5711a1de3fee16009de.png)

---

<div class="post-metadata">

**Author:** ![evaldas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/evaldas/32/69160_2.png) [@evaldas](https://community.hubspot.com/u/evaldas)\
**Post date:** [December 18, 2024, 7:10pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/2 "2024-12-18T19:10:55Z")

</div>

Hi @SMCNULTY,

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

```lia-code-sample
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:

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

```

---

<div class="post-metadata">

**Author:** ![SMCNULTY](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@SMCNULTY](https://community.hubspot.com/u/SMCNULTY)\
**Post date:** [December 18, 2024, 7:59pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/3 "2024-12-18T19:59:15Z")

</div>

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”

 ![SMCNULTY_0-1734551928509.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/3/6/36aea18c2069b092cbe79996996acf6963ad7b35.png)

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

 ![SMCNULTY_0-1734552604832.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/b/b/bbb6aa1928c8eb91fb25ee8c72dab9b2b39a4279.png)

---

<div class="post-metadata">

**Author:** ![evaldas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/evaldas/32/69160_2.png) [@evaldas](https://community.hubspot.com/u/evaldas)\
**Post date:** [December 18, 2024, 8:12pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/4 "2024-12-18T20:12:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SMCNULTY](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@SMCNULTY](https://community.hubspot.com/u/SMCNULTY)\
**Post date:** [December 18, 2024, 9:40pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/5 "2024-12-18T21:40:45Z")

</div>

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)

 ![SMCNULTY_0-1734556955433.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/a/ea1451ef1c51844e25c637bd4a07400a9903359e.png)

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)

 ![SMCNULTY_1-1734557066368.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/f/9ff3361e37d1a486c41d6c142dd4bd035ec5ae7c.png)

 ![SMCNULTY_2-1734558098130.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/d/d27f4dec7ffd8e04aa7bd2c0802a3c4b75a88e6a.png)

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.

 ![SMCNULTY_0-1734558900473.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/b/3/b3c8b7682d917b1cb684a6dbc3518ee9ba8a103a.png)

Here is Postman code that works:

 ![SMCNULTY_0-1734559171662.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/0/0495c4141c0a137c8de6eab9992d45a9e7555e25.png)

---

<div class="post-metadata">

**Author:** ![evaldas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/evaldas/32/69160_2.png) [@evaldas](https://community.hubspot.com/u/evaldas)\
**Post date:** [December 18, 2024, 10:00pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/6 "2024-12-18T22:00:40Z")

</div>

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.

```lia-code-sample
console.log("Kone po: " + kone_po_changes);
console.log("PO: " + p_o_);

```

---

<div class="post-metadata">

**Author:** ![SMCNULTY](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@SMCNULTY](https://community.hubspot.com/u/SMCNULTY)\
**Post date:** [December 18, 2024, 10:19pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/7 "2024-12-18T22:19:16Z")

</div>

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.

 ![SMCNULTY_0-1734560296890.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/1/8/1890162c4e2e17b247a307fa521eda7e93f3aa0c.png)

 ![SMCNULTY_1-1734560319662.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/f/a/fabc797675e304ec3c8d9bf36db3eee59fd29e3c.png)

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

---

<div class="post-metadata">

**Author:** ![evaldas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/evaldas/32/69160_2.png) [@evaldas](https://community.hubspot.com/u/evaldas)\
**Post date:** [December 19, 2024, 1:50am UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/8 "2024-12-19T01:50:25Z")

</div>

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)

```lia-code-sample
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_
 }
 ]
});

```

---

<div class="post-metadata">

**Author:** ![SMCNULTY](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@SMCNULTY](https://community.hubspot.com/u/SMCNULTY)\
**Post date:** [December 19, 2024, 7:56pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/9 "2024-12-19T19:56:37Z")

</div>

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.

 ![SMCNULTY_0-1734638181945.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/3X/3/4/34121e89d0516f5725fd97eafae730d7f71d18ef.jpeg)

---

<div class="post-metadata">

**Author:** ![evaldas](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/evaldas/32/69160_2.png) [@evaldas](https://community.hubspot.com/u/evaldas)\
**Post date:** [December 19, 2024, 8:18pm UTC](https://community.hubspot.com/t/insert-variable-into-json/122755/10 "2024-12-19T20:18:51Z")

</div>

Awesome! Glad to hear it worked 🙂
