Skip to main content
Solved

How to escape " inside JSON into a webhooks


Forum|alt.badge.img+1

Hello, i’ve a contact form, made in Klaviyo that allow me through webhooks to register contact form details in Google Sheets.

 

Unfortunately if in the field request of the contact form i insert this special char “ the json of the webhooks gave me: Invalid JSON, how can i escape this special char? i tried with this syntax but it doesn’t works :-(

 

"request": "{{ person.request|replace:'\"','\\\"' }}",

 

Thank you

Vincenzo

Best answer by stucco81

I solved it by adding this filter to request


  "request": "{{ person.request|escape }}"

 

Thank you 

Vincenzo

View original
Did this topic or the replies in the thread help you find an answer to your question?

4 replies

Brian Turcotte
Forum|alt.badge.img+37

Hi @stucco81!
 

Webhooks use JSON language in the payload, for which there are certain rules that are not necessarily managed by Klaviyo. I would recommend searching for third-party resources about escaping special character, such as this:

 

Best,

Brian


Forum|alt.badge.img+1
  • Author
  • Problem Solver I
  • 3 replies
  • July 21, 2023

Hi brian, i’ve tried to insert something more like this:
 

const person = {
  "first_name": "John",
  "last_name": "Doe",
  "phone_number": "123456789",
  "email": "john.doe@example.com",
  "request": "This is a request with a special character: \"",
  "subject": "Some subject",
  "consent": "2023-07-21 12:34:56",
  "language": "English"
};

const jsonString = JSON.stringify(person);

console.log(jsonString);
 

 

But when i try to preview the webhook system continue to give me that error (invalid JSON)


 


Brian Turcotte
Forum|alt.badge.img+37

Hi @stucco81!

 

Would be willing to share your webhook payload (with any sensitive info redacted)? 

 

When we tested this with a webhook call to our Create Event endpoint, we included the special character “ in a custom property, and the webhook was successful and updated the profile as expected:

{
  "data": {
    "type": "event",
    "attributes": {
      "properties": {
        "newKey": "New Value"
      },
      "metric": {
        "data": {
          "type": "metric",
          "attributes": {
            "name": "test_event"
          }
        }
      },
      "profile": {
        "data": {
          "type": "profile",
          "attributes": {
            "properties": {
              "test_prop3": "brian\"stest"
            },
            "email": "brianklaviyo+july27@gmail.com"
          }
        }
      }
    }
  }
}

(payload)

 

And here is the corresponding property in the Klaviyo UI:

 

Are you making the API call to a Klaviyo endpoint, or a third-party endpoint? It looks like the standard escape practices worked when making a call to a Klaviyo API endpoint. 

 

Best,

Brian Turcotte


Forum|alt.badge.img+1
  • Author
  • Problem Solver I
  • 3 replies
  • Answer
  • July 28, 2023

I solved it by adding this filter to request


  "request": "{{ person.request|escape }}"

 

Thank you 

Vincenzo