Webhook addon: Formatting json in body [REOPENED]

Hi there,

I’m trying to use the webhook addon to send a post request with a json body to an API. For the most part everything works, except I can’t workout how to format the json with nested properties. The snippet below shows the format I’m trying to achieve but which I cannot workout how to get the syntax correct.

{
“summary”: “testing”,
“company”:
{
“id” : 19853
},
“contact”: {
“id” : 1186
},
“initialDescription”: “Hello testing initial description”,
“initialInternalAnalysis”: “powesrshell goes here”
}

I have created the keys as shown below but this does not give the output I had hoped for. Can anyone shine some light on how I might produce the json above?

image

1 Like

I’m afraid it’s not currently possible to define that structure via the feed settings, we do have it on the feature request list for consideration by the development team when planning future versions of the add-on.

You could use the gform_webhooks_request_data filter in the theme functions.php file or a custom functionality plugin to create your own array of data, including using nested arrays. The array you return via the filter will then be json encoded by the add-on before being sent to the designated endpoint.

Hey All,
I am having the same issue as well, and just came across this thread. I have my webhook ALMOST working, I am almost there! I’m not sure how I would use the gform_webhooks_request_data filter to pass the emails array in my example below to my API. I read the documentation, and since this is all new to me, the documentation examples didn’t really help. Looking for some guidance.
Dave

{
“firstName”: “James”,
“lastName”: “Gordon”,
“emails”: [
{
“type”: null,
“email”: "jim@gotham.city.us",
“dateCreated”: “2004-10-31T14:35:00Z”,
“isPreferred”: true
}
]
}

I ended up purchasing the gravityflow plugin which made this whole thing much easier. I recommend giving it a go

1 Like

Basically, I am looking for help from the community for how to use the gform_webhooks_request_datato format JSON Body with nested arrays.

Just need some help & guidance on how I would use this to do this. I’ve already configured most of the webhook, just need to figure out this last part.

Curious if anyone else has use the gform_webhooks_request_data like this before, and if they can offer some guidance to someone new to this.

+1 for more control of the json body (maybe a preview as well) in the webhook setup UI. Nested JSON is pretty common on most APIs.

In mean time, i usually use gform_after_submission combined with wp_remote_post after i have formatted my json how i need it incase it helps anyone

1 Like

I am not very techy but I found a solution that worked for me. On the Request data page there is an example for adding a new field (#1). When I used this it overrides the body field settings from the webhooks add-on. I don’t really understand why it’s not possible to add fields to the body instead of overriding but now I just added all the fields I need to send in JSON format to the third party API and this worked perfectly.

So for example, within the function just add:

$request_data = array (
'contact' => array (
        'first_name' => rgar( $entry, 'your field_ID' ),
        'last_name' => rgar( $entry, 'your field_ID' ),
        'message' => rgar( $entry, 'your field_ID' ),
        );
);
return $request_data;

Do this for every field you want to add to the body.
Hope this helps anyone and if someone has a better solution, for example on how to add or modify the body without overriding it, I would like to know.

Hey Joe, I kind of get what your doing there. I need the request data to look like this:

{

“data”: {

"type": "Leads",

"attributes": {

  "name": "Postman Lead",

  "email1": "testemail@testing1.com",

  "account_name": "Postman",

  "first_name":"Pablo",

  "last_name": "Stevens2"

}

}

}

How did you structure your request data to have all the right formatting?