Best answer

Nested JSON in Action Step

  • 23 July 2020
  • 2 replies
  • 1975 views

Userlevel 7
Badge +10

This is the code that I’m using to call my API Endpoint:

 

And here’s the actual code:

body: {
"time_entry_sets": [
{
"employee": {
"account_id": bundle.inputData.account_id
},
"start_date": bundle.inputData.Start_Date,
"end_date": bundle.inputData.End_Date,
"time_entries": bundle.inputData.time_entries
}
]

}

 

bundel.inputData.time_entries will have a JSON Array or Object passed to it like so:

I used single quotes in here just to play it safe. Ideally I’d like to use the standard quotes but the platform escapes them.

In code that JSON is: 

[{'date': '2020-07-08', 'type': 'TIME', 'total': 18000000, 'pay_category': { 'id': 35122012 } }, {'date': '2020-07-09', 'type': 'TIME', 'total': 18000000, 'pay_category': { 'id': 35122012 } }]

 

My issue is that the Zapier Platform puts quotes around any variiable I put into the body JSON, in this case bundle.inputData.time_entries shouldn’t have a quote around it because it’s not processing correctly in the API I’m receiving. I may be “valid” JSON because the object or array just becomes a quoted string. 

Here’s what is sent to my API:

{
"time_entry_sets": [{
"employee": {
"account_id": "4333592980"
},
"start_date": "2020-07-05",
"end_date": "2020-07-18",
"time_entries": "[{'date': '2020-07-08', 'type': 'TIME', 'total': 18000000, 'pay_category': { 'id': 35122012 } }, {'date': '2020-07-09', 'type': 'TIME', 'total': 18000000, 'pay_category': { 'id': 35122012 } }]"
}]
}

Here’s what I WANT it to send:

{
"time_entry_sets": [{
"employee": {
"account_id": "4333592980"
},
"start_date": "2020-07-05",
"end_date": "2020-07-18",
"time_entries": [{
"date": "2020-07-08",
"type": "TIME",
"total": 18000000,
"pay_category": {
"id": 35122012
}
}, {
"date": "2020-07-09",
"type": "TIME",
"total": 18000000,
"pay_category": {
"id": 35122012
}
}]
}]
}

The only difference is that I changed the single quotes (‘) to double quotes (“) and I removed the double quotes that Zapier added in before the “[{“ and after the “}]” in the time_entry value. 

Hopefully I’m making sense. 

 

Here’s the alternatives I’ve tried:

  • Using `${bundle.inputData.time_entries}` (no change in output here)
  • Using a variable outside of this const body like let time_entries= bundle.inputData.time_entries and then just replace the row 25 with time_entries and Zapier turns that into “time_entries”:”(the nested JSon)” which gives the same output/problem
  • JSON.stringify or JSON.parse
  • bundle.inputDataRaw.time_entries This had some promise I thought because Zapier wouldn’t process it, but nope it still adds in the quotes. 

Thanks in Advance for the help!

 

icon

Best answer by ikbelkirasan 23 July 2020, 04:23

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +12

Hi @PaulKortman - That happens because the JSON object/array you’re passing as input is passed by Zapier a string rather than an object/array. So, you would need to parse it first. JSON.parse won’t work in this case because the example input you provided isn’t a valid JSON because of the single quotes. Thus, you should rather be using double quotes when filling in time_entries field, like so:

[{"date":"2020-07-08","type":"TIME","total":18000000,"pay_category":{"id":35122012}},{"date":"2020-07-09","type":"TIME","total":18000000,"pay_category":{"id":35122012}}]

Then in your code, JSON.parse should work now:

{
//...
body: {
time_entry_sets: [
{
employee: {
account_id: bundle.inputData.account_id,
},
start_date: bundle.inputData.Start_Date,
end_date: bundle.inputData.End_Date,
time_entries: JSON.parse(bundle.inputData.time_entries),
},
],
},
}

Hope this helps!

Userlevel 7
Badge +10

Thanks @ikbelkirasan that led me down the road I needed to take… when I had previously tried JSON.parse I was sending it the value without the square brackets around it, so it wasn’t parsing (a string of objects instead of an array of objects) but by trying again this time with the square brackets in it worked like it was supposed to :)