JSON formatting on POST request

Hi all,

I’ve been pulling what’s left of my hair out over this. I need to send a JSON payload that has nested values to a third party software but there seems to be no way to do it in the workflow.

I tried doing it using the Send webhook action but that only allows one level, no nesting. I tried a few other methods to put the nested value together as a string but Hubspot just injects backslashes into it so it becomes unusable.

I looked through the forums, and have contacted support. Everything seems to point to this thread, but I tried both solutions and neither worked.

https://community.hubspot.com/t5/APIs-Integrations/Workflow-webhooks-with-nested-parameters/m-p/752643#M61178
I added a code block using Python that used requests to make the post request and it still inserts backslashes. I tried running the code on my own compiler so I know it’s not an issue there.

I also tried using a Format data action but this had the same problem, I ended up with back slashes, and on top of that it adds this ‘“formula_result_output”:’ to the start of the string.
There must be some way to send nested JSON, does anyone have a solution?

Thanks in advance!

Hello @MClubb

HubSpot does not support nested JSON in workflow actions. This is because JSON is a string, and strings in HubSpot are escaped by default. This means that any backslashes in the string will be converted to double backslashes, which will break the nested JSON. use a custom code block. In the custom code block, you can use the requests library to make a POST request to the third-party software. The requests library allows you to send nested JSON without having to escape backslashes.

import requests

def send_nested_json(url, json_data):
 response = requests.post(url, json=json_data)
 return response

if __name__ == "__main__":
 url = "https://example.com/api/v1/my-endpoint"
 json_data = {
 "nested_field": {
 "key1": "value1",
 "key2": "value2"
 }
 }
 response = send_nested_json(url, json_data)
 print(response.content)

This code will send the nested JSON data to the third-party software. The requests library will automatically escape backslashes, so you don’t need to worry about that.