Best answer

Malformed JSON between Zapier webhook POST / Catch Raw Hook

  • 18 April 2021
  • 8 replies
  • 1478 views

Userlevel 1

I’m have a Custom Request webhook action w/ Data Pass-Thru enabled, sending data to a Catch Raw Hook trigger. I can see everything sent in the raw_body field of the Zap with the Catch Raw Hook trigger, but the JSON is malformed: it uses ‘ instead of “ for both the key names and for the string-typed fields in the JSON object.

This obviously makes it not possible to get at the data via JSON.parse() in a subsequent Run Javascript action.

Why is the raw_body field not valid JSON and how do I fix it?

Note that “just globally search and replace single quotation marks to double quotation marks” is not a valid solution, as that will tamper with legit string-typed JSON fields that have double quotes.

I also feel like I shouldn’t have to do ad-hoc munging of the data from Zapier to itself...

icon

Best answer by azcwebmanager 17 June 2021, 06:22

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.

8 replies

Userlevel 7
Badge +12

While not ideal, you could try replacing each ‘ as it exists in a group

function formatMalformedJSON(string) {
while (string.includes("{'"))
string = string.replace("{'",'{"')

while (string.includes("'}"))
string = string.replace("'}",'"}')

while (string.includes("':"))
string = string.replace("':",'":')

while (string.includes(":'"))
string = string.replace(":'",':"')

while (string.includes("',"))
string = string.replace("',",'",')

while (string.includes(",'"))
string = string.replace(",'",',"')

return string;
}

 

Userlevel 7
Badge +12

@azcwebmanager - You’ll need to use a Python code step to parse & unflatten the raw body.

Here’s a code snippet that you can use to do that:

def unflatten(dictionary, delimiter="__"):
result = {}
for key, value in dictionary.items():
parts = key.split(delimiter)
tmp = result
for part in parts[:-1]:
if part not in tmp:
tmp[part] = {}
tmp = tmp[part]
tmp[parts[-1]] = value
return result


data = eval(input_data["raw_body"])
output = unflatten(data)

 

Userlevel 1

Thanks for the helpful suggestions, folks -- much appreciated! I really didn’t want to have to execute adhoc fixup code but it’s good to know that it’s possible if it comes to that.

However, I was able to get confirmation from Zapier technical support that this malformed JSON is a bug that they intend to fix ASAP.

In my case, the problematic behavior comes from a Zapier action for sending a webhook with “Custom Request” selected + “data pass-through” checked, sending to a webhook Zap with “Catch Raw Hook” enabled. Tech support pointed out that -- in my particular case -- I didn’t actually need to rely on the “Custom Request” mode on the calling side, and that just a POST request w/ JSON payload selected (and everything else default) indeed does pass the entire payload through without explicit field mappings (which I thought might be needed, and thus I had originally avoided). 

So, if someone runs into this same issue -- and doesn’t need the “Custom Request” functionality -- doing the above did provide me with a suitable interim solution (the “Catch Raw Hook” trigger witnesses the entire payload and the JSON is not malformed).

 

Userlevel 7
Badge +10

@azcwebmanager 
Just checking in to see if you still need help with this? 

Userlevel 1

@AndrewJDavison_Luhhu Not too terribly happy about the GravityForms ↔ Zapier integration, but we’ve made do and I’m no longer troubled by the above issue. Thanks for checking! =)

Userlevel 1

@AndrewJDavison_Luhhu are you a zapier employee? the issue still exists, passing data via a custom webhook and the pass-thru option results in malformed json

How comes after one year?

I rely on being able to pass the data to an external application that requires well formed json, how can we move forward without this bug being resolved?

Userlevel 1

Just met this bug. Is there a workaround? I tried to substitute Custom Request with native Get and json, but did get the same single quote result

Userlevel 1

no workaround so far, I’m repeating each json field in a call, that’s bad zapier is not taking this seriously