Webhook sending escape characters

Hi,

I am trying to send data using a webhook to a website, but upon receiving the data, I noticed that I am receiving escape characters ("\") along with the data. According to the documents, I should not be receiving the escape characters.

The output I would like to see is within the data is:
"data": "{"Lat":000.0000, "Long":000.0000}",

But, instead I am getting:
"data": "{\"Lat\":000.0000, \"Long\":000.0000}",

Here is my webhook, firmware and output:

Webhook:

{
    "event": "Sensible - Test2",
    "url": "-----",
    "requestType": "POST",
    "noDefaults": true,
    "rejectUnauthorized": false,
    "json": {
        "source_device": "{{{PARTICLE_DEVICE_ID}}}",
        "datetime": "{{{PARTICLE_PUBLISHED_AT}}}",
        "data": {
            "Lat": "{{{latx}}}",
            "Long": "{{{longx}}}"
        }
    }
}

Firmware:

String test_message = String::format (
    "{\"Lat\":%f, \"Long\":%f}",
    latx, longx
);
bool success = Particle.publish("Sensible - Test2", test_message, PRIVATE);

Output:

{
  "name": "Sensible - Test2",
  "data": "{\"Lat\":000.0000, \"Long\":000.0000}",
  "ttl": 60,
  "published_at": "2018-09-20T18:05:08.806Z",
  "coreid": "----"
}

How should I go about fixing this problem?

Thanks,
Jeffrey

Are you sure you actually get the escaped characters and that this isn’t only the way the console displays the string?

Also when you are naming your keys Lat and Long you should reference them in your webhook as {{Lat}} and {{Long}}

1 Like

According to my customer on the other end, he is obtaining the escape characters

We have had this problem before and it has always turned out that the receiver is printing the JSON with escaped characters in order to be “helpful.”

I would urge you to fire up curl and look at your event stream for yourself. See the doc for how to do that.

2 Likes

This is the output from curl:

data: {"data":"{\"Lat\":00.0000, \"Long\":-000.0000}","ttl":60,"published_at":"2018-09-20T18:35:08.509Z","coreid":"---"}

So that looks correct. Because the keys are in a quoted list, their own quotes need to be escaped.

Have you tried updating the webhook keys as @ScruffR pointed out?

This is the correct thing for the console and curl to display:

"data": "{\"Lat\":000.0000, \"Long\":000.0000}",

The reason is the data is a string, not a JSON object. Thus it’s surrounded by double quotes, and the double quotes are escaped.

In order to use the data you must JSON parse the data field before using it. In Javascript use JSON.parse(), but you can do a similar thing in other languages.

2 Likes