Best answer

Data from Webhook

  • 8 August 2023
  • 7 replies
  • 87 views

Userlevel 1

Hello! New to Zapier and loving it so far. I’ve run into an issue and I cannot seem to resolve. 

I’m receiving the below example JSON from a webhook I have within Zapier. I cannot figure how to split the interview questions and answers into fields that I can import into my CRM. Additionally, some of the interview questions/answers are not always present.

Ideally, I’d like to collect each question & answer so I can import the respective bit of information into my CRM. I’m stuck. Halp!

 

{
    "firstName": "PersonName",
    "interview": [
        {
            "answer": "school name here",
            "question": "Name of College"
        },
        {
            "answer": "1985",
            "question": "Graduation Year"
        },
        {
            "answer": "Yes",
            "question": "Degree"
        },
        {
            "answer": "Planning & Budgeting",
            "question": "Request Stage"
        },
        {
            "answer": "Timing is flexible",
            "question": "Desired Completion Date"
        },
        {
            "answer": "Home/Residence",
            "question": "Location"
        },
        {
            "answer": "No",
            "question": "Covered by Insurance"
        }
    ],
    "lastName": "PersonLastName",
}

icon

Best answer by Todd Harper 8 August 2023, 20:52

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.

7 replies

Userlevel 6
Badge +8

You can add the following JS in a code step, using the comma separated list of Interview answers as the input data (and a key word of “answers”):

output = {answers: ""};
const answers = inputData.answers.split(",");
output.answers = answers;

Alternatively, if you’d like each item named for easier recognition in later steps:

output = {schoolName: "", graduationYear: "", degree: "", requestStage: "", desiredCompletion: "", location: "", covered: ""};

const answers = inputData.answers.split(",");
output.schoolName = answers[0];
output.graduationYear = answers[1];
output.degree = answers[2];
output.requestStage = answers[3];
output.desiredCompletion = answers[4];
output.location = answers[5];
output.covered = answers[6];

Here’s what it looks like when adding it to subsequent steps (note that I left “graduation year” blank to demonstrate that you can still select it when an answer is not present, but it just won’t have an assigned value):

Though now I’m rereading your question and see that you mentioned sometimes the question AND answer aren’t present, not just the answer. That gets a little more tricky as you may need to write some additional logic to check “If ? exists, answer = ???”.

Userlevel 7
Badge +14

Hi @Matthue13 

Another approach is to use to parse raw JSON, is to use this Zap action: ChatGPT - Extract Structured Data

Userlevel 6
Badge +8

Though now I’m rereading your question and see that you mentioned sometimes the question AND answer aren’t present, not just the answer. That gets a little more tricky as you may need to write some additional logic to check “If ? exists, answer = ???”.

Here is some sample code to get you started on that additional logic if you go the code route:

output = {schoolName: "", graduationYear: "", degree: "", requestStage: "", desiredCompletion: "", location: "", covered: ""};

const answers = inputData.answers.split(",");
const questions = inputData.questions.split(",");

for (let i in questions) {
if (questions[i] == "Name of College") {
output.schoolName = answers[i];
}
if (questions[i] == "Graduation Year") {
output.graduationYear = answers[i];
}
//etc.......
}

 

Userlevel 1

Though now I’m rereading your question and see that you mentioned sometimes the question AND answer aren’t present, not just the answer. That gets a little more tricky as you may need to write some additional logic to check “If ? exists, answer = ???”.

Here is some sample code to get you started on that additional logic if you go the code route:

output = {schoolName: "", graduationYear: "", degree: "", requestStage: "", desiredCompletion: "", location: "", covered: ""};

const answers = inputData.answers.split(",");
const questions = inputData.questions.split(",");

for (let i in questions) {
if (questions[i] == "Name of College") {
output.schoolName = answers[i];
}
if (questions[i] == "Graduation Year") {
output.graduationYear = answers[i];
}
//etc.......
}

 

Thanks for the quick response, Todd! When trying your code, I get an error from Zapier saying the below.

 

 

I tried one field to test. See sample code below:

 

 

Userlevel 6
Badge +8

@Matthue13 Hmm...that’s pretty odd! Can you send a screenshot of what appears if you just type the following?

output = {answers: "", questions: ""};

output.answers = inputData.answers;
output.questions = inputData.questions;

 

Userlevel 1

@Matthue13 Hmm...that’s pretty odd! Can you send a screenshot of what appears if you just type the following?

output = {answers: "", questions: ""};

output.answers = inputData.answers;
output.questions = inputData.questions;

 

I figured out the issue. It was an ID10T error on my part 😂

 

To clarify, I copied your original code with the if statements and modified specifically to my use case. Everything worked perfectly this morning. I think I just needed a coding break. I’m a C# guy and not 100% familiar with Zapier and JavaScript as of yet.

 

Thanks for the expert advice and guidance! 

Hello! It's fantastic to hear that you're enjoying your Zapier experience. Addressing your issue with splitting interview questions and answers from a webhook's JSON can indeed be a puzzle.

For your specific case, I'd suggest creating a loop within your Zap to process each interview question-answer pair. This way, you can extract and map each piece of information individually into your CRM fields.

Since some questions may not always be present, consider implementing conditional logic to handle those cases gracefully. This ensures that the import process remains smooth and accurate even when certain data points are missing.

Remember, Zapier's support team could offer tailored guidance, especially since this scenario requires a bit of fine-tuning. You're doing great – keep going and soon you'll have an updated system that handles the entireresult seamlessly!