Trouble with JSON parsing in Zapier webhook integration

I’m trying to set up a data sync between two CRM systems using Zapier and webhooks. The source CRM sends data in JSON format, but I’m running into issues when trying to parse it in Zapier.

Here’s a simplified version of the JSON I’m receiving:

{
  "targetId": 9876543,
  "profiles": [
    {
      "customerInfo": {
        "name": "John Smith",
        "email": "[email protected]",
        "customerId": "12345"
      },
      "objectId": "abc123def456",
      "identity": "12345"
    }
  ]
}

I attempted both the regular Catch Hook and the Catch Raw Hook. The Raw Hook appears to handle the JSON correctly, but when I try to access the profiles array in a code step, I encounter an error stating that profiles[0] is undefined.

My code step currently looks like this:

let data = JSON.parse(JSON.stringify(inputData));
console.log(data.profiles[0]);
return data;

The regular Catch Hook, however, parses the data in a way that prevents JSON.parse from working as expected. Does anyone have suggestions on how to reliably access the profiles data using Zapier webhooks? Any help would be greatly appreciated. Thanks!

hey there! i’ve run into this before. try using the ‘utilities’ step in zapier. it has a ‘parse JSON’ option that works great for complex structures. just pop that in before ur code step and it should sort things out. if not, hit me up and we can brainstorm more ideas!

I’ve dealt with similar JSON parsing issues in Zapier before, and it can be tricky. One thing that’s worked for me is using the ‘Formatter’ step before your code step. It has a ‘Text’ function that can parse JSON.

Try adding a Formatter step right after your webhook, set it to ‘Text’ and choose ‘Parse JSON’. Input your raw webhook data there. Then, in your code step, you should be able to access the parsed data directly.

If that doesn’t work, another approach is to use the lodash library that Zapier provides. You could try something like:

const _ = require('lodash');
let profiles = _.get(inputData, 'profiles', []);
console.log(profiles[0]);
return profiles;

This method is more robust and can handle cases where the ‘profiles’ key might not exist. It’s been a lifesaver for me when dealing with inconsistent JSON structures. Good luck with your integration!

I’ve encountered similar issues with Zapier’s JSON parsing before. From my experience, the Raw Hook is usually the way to go for complex JSON structures. However, your current approach might be causing unnecessary parsing.

Try simplifying your code step like this:

let data = inputData;
console.log(data.profiles[0]);
return data;

The Raw Hook should already give you a parsed JavaScript object, so you don’t need to stringify and parse it again. This extra step might be causing the undefined error.

If that doesn’t work, you could try logging the entire inputData to see its structure:

console.log(JSON.stringify(inputData, null, 2));

This will give you a clearer picture of how Zapier is receiving the data. From there, you can adjust your code accordingly. Hope this helps!