Skip to main content

Web form Json payload structure conversion to Zapier required structure

  • August 8, 2024
  • 1 reply
  • 27 views

Hi 

Looking for a possible way to use an existing Json payload from a webform where the strucutre is different from what Zapier’s required structure is, please see below the examples: 

Our web forms provided structure: 

{

   "Fields":[{ "key":"firstName","value":"Michael" },{"key":"lastName","value":"Scott" },{"key":"email","value":email@domain.com }]

}


Zapier required structure: 

 

{
    "name": "Example Name",
    "email": “example@gmail.com”,
}

Would it be possible to convert the structure in Zapier to the required one??

Thanks!

Did this topic help you find an answer to your question?

1 reply

Badger
Forum|alt.badge.img+5
  • New
  • 134 replies
  • November 25, 2024

@andrismalna 
I believe it is possible to do. You would need a code block and I’ve two AI generated samples below one showing how to take from to zapier and the other zapier to form. 

In the zapier to form example I have included the output as a text string in case the next step errors due to format. 

 

import json

# Input data from the web form
web_form_data = {
    "Fields": [
        {"key": "firstName", "value": "Michael"},
        {"key": "lastName", "value": "Scott"},
        {"key": "email", "value": "email@domain.com"}
    ]
}

# Convert to Zapier structure
name = ""
email = ""

# Iterate through the fields to find required keys
for field in web_form_data["Fields"]:
    if field["key"] == "firstName":
        name = field["value"]  # Start with firstName
    elif field["key"] == "lastName":
        name += f" {field['value']}"  # Append lastName
    elif field["key"] == "email":
        email = field["value"]

# Create the required structure
required_structure = {
    "name": name.strip(),
    "email": email
}

# Output the required structure
output = required_structure

 

import json

# Input data from Zapier
zapier_data = {
    "name": "Michael Scott",
    "email": "email@domain.com"
}

# Split the name into first and last name (assuming first and last names are separated by a space)
name_parts = zapier_data["name"].split(" ")
first_name = name_parts[0]
last_name = " ".join(name_parts[1:]) if len(name_parts) > 1 else ""

# Build the web form structure
web_form_structure = {
    "Fields": [
        {"key": "firstName", "value": first_name},
        {"key": "lastName", "value": last_name},
        {"key": "email", "value": zapier_data["email"]}
    ]
}

# Convert the web form structure to a JSON string
web_form_structure_text = json.dumps(web_form_structure, indent=4)

# Output the JSON object and the text string
output = {
    "web_form_structure": web_form_structure,
    "web_form_structure_text": web_form_structure_text
}

I hope this is useful.