Skip to main content
Best answer

Malformed JSON between Zapier webhook POST / Catch Raw Hook


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...

Best answer by azcwebmanagerBest answer by azcwebmanager

@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! =)

View original
Did this topic help you find an answer to your question?
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

GetUWired
Forum|alt.badge.img+12
  • Zapier Expert
  • 1030 replies
  • April 20, 2021

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;
}

 


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 20, 2021

@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)

 


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).

 


AndrewJDavison_Luhhu
Forum|alt.badge.img+10

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


  • Author
  • Beginner
  • 3 replies
  • Answer
  • June 17, 2021

@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! =)


  • Beginner
  • 3 replies
  • October 4, 2022

@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?


  • New
  • 1 reply
  • December 23, 2022

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


  • Beginner
  • 3 replies
  • December 23, 2022

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