Webhook not parsing data JSON

This is my webhook definition:

{
    "event": "measure",
    "url": "http://www.mysite.com/v1/measure",
    "requestType": "POST",
    "mydevices": true
}

And my firmware code:

Particle.publish(
    "measure", 
    "{\"value1\": " + String(currentValue1, 2) + 
        ", \"value2\": " + String(currentValue2, 2) +
        "}",
    60,
    PRIVATE
);

My problem is that in the server I get the “data” field as a plain string containing the JSON:

{ event: 'measure',
  data: '{"value1": 31.00, "value2": 30.00}',
  published_at: '2015-08-31T02:47:59.976Z',
  coreid: '1axxxxxxxxxxxxxxxxxxxxxxxxx' }

Am I supposed to parse it manually on server side or it should be already come in JSON format?

Thanks

Your event looks very much like a custom template, so you should provide a definition for it in your webhook.
You could either “hand parse” it yourself or provide the full webhook definition.

Have a look at the docs here
https://docs.particle.io/guide/tools-and-features/webhooks/#custom-template-variables

Hi @javo,

I think you mean:

"{\"value1\": \"" + String(currentValue1, 2) + "\"," + 
        "\"value2\": \"" + String(currentValue2, 2) + "\"}", 

you want the output to be:

 {"value1": "some string", "value2": "some other string" }

and not:

 {"value1": some string, "value2": some other string }

Thanks!
David

2 Likes

You are right. I totally overlooked that. Thanks!
EDIT: See below

Wait a second… no. I’m using String here to limit the precision digits, not because the values are strings. They are always numbers and as far as I know they are valid in a JSON file.

My output is:

    {"value1": 20.14, "value2": 19.51 }

Ah, nevermind then! If they’re literals and not strings, then you’re right, quotes wouldn’t be needed. :slight_smile:

Whatever gets published, you might want to check it in a JSON validator ( http://jsonlint.com/ ).

In your hook, use the json property in the place where you want the value to be. The hook treats it like a mustache template, so something like:

{
    "event": "measure",
    "url": "http://www.mysite.com/v1/measure",
    "requestType": "POST",
    "json": {
      "value1": "{{value1}}"
    },
    "mydevices": true
}

Thanks!
David

1 Like

That must be it. Now I notice that my webhook definition was not correct. I must confess the docs are a bit confusing on this subject.

1 Like

Thanks, I’ll continue to try and improve the docs! :slight_smile:

Hi @Dave
When I provide the coordinates manually in webhook url , I got my required offset i.e -7 from the response template. But my problem is I need to update the url with the coordinates of my photon device automatically. With the help google maps api i used to get the coordinates of my photon
device. In the 2nd screen shot u can see my location " locationCallback 12.924183,77.552666"



Can you please help me with this regard

It looks like you just need to replace the hardcoded coordinates in the URL field for the locationCallback webhook with {{PARTICLE_EVENT_VALUE}}.

https://api.darksky.net/forecast/6f.....c8/{{{PARTICLE_EVENT_VALUE}}
3 Likes

Thanks a lot @rickkas7 :sweat_smile: for your kind help. It works like a charm. I got the output what i want.

One more request @rickkas7 , how can I store or save the offset response value so that i can use that in my coding.

Yeah I figured it out finally. So no worries. Thanks for the kind help @rickkas7 :smile:
Thanks,
Imran

1 Like