Do not understand how to handle the JSON using Losant

#1

Hello,
Using Workflow to fetch data from my Cloud once every 10 minutes. It is a GET.
Data is coming fine and using now Virtual Button, HTTP, Debug, Store Value and on the same level Email.
I have no idea on how to get my JSON response and store it or send it to the email. Do not understand what is the format.
For instance if I want to save all the “temp” from all arrays, what is the format?
Sorry I am not to Kean in using JSON yet.
Following is my response:

{
  "time": "2016-04-11T14:34:04.584Z",
  "data": {},
  "applicationId": "xxxx",
  "triggerId": "xxxxx",
  "triggerType": "virtualButton",
  "flowId": "xxxx",
  "globals": {},
  "body": {
    "body": {
      "body": [
        {
          "version": [
            "Cloud, xxx 0.5"
          ]
        },
        {
          "lon": "10.88238",
          "temp": "20.7",
          "idsensor": "S2",
          "date": "Mon Apr 11 14:33:46 UTC 2016",
          "lat": "45.97111"
        },
        {
          "lon": "10.97",
          "temp": "22.0",
          "idsensor": "S4",
          "date": "Mon Apr 11 14:30:35 UTC 2016",
          "lat": "50.019"
        },
        {
          "lon": "3.53",
          "temp": "21.8",
          "idsensor": "S5",
          "date": "Mon Apr 11 14:28:19 UTC 2016",
          "lat": "50.37"
        },
        {
          "lon": "3.93",
          "temp": "30.8",
          "idsensor": "Test",
          "date": "Mon Jan 18 11:01:11 UTC 2016",
          "lat": "53.9922"
        }
      ],
      "headers": {
        "content-type": "text/plain; charset=iso-8859-1",
        "date": "Mon, 11 Apr 2016 14:34:04 GMT",
        "server": "Google Frontend",
        "content-length": "432",
        "connection": "close"
      },
      "statusCode": 200
    }
  }
}
#2

Most fields in workflows are “template” fields, which allow you to reference values on the payload by surrounding them in double curly braces. So for example, if you want to send an email with the four “temp” values, it would look like this:

The three temp values are:
{{ body.body.body[1].temp }}
{{ body.body.body[2].temp }}
{{ body.body.body[3].temp }}
{{ body.body.body[4].temp }}

The templates use mustache, so there’s actually a lot you can do to pull values from the payload.

The paths (e.g. body.body.body) correspond to nesting inside the JSON object. As you can see your specific payload has a body object inside another body object inside another body object, so you’d reference that using body.body.body. The inner-most body is an array, so you can reference objects on the array using the [ ] syntax. The first element (index 0) has the “version” information, which is why my example email starts at [1].

#3

If you want to store the values for use in dashboard visualizations, that would require a virtual device. We have a walkthrough that pulls weather data from forecast.io and visualizes it. I think there’s a lot of similarities with what you’re doing. I would highly recommend checking it out.

#4

Great, thank you Brandon for the quick reply. I knew I need to study more JSON.
Why if I select JSON as a value type it says “Invalid JSON”?
Is this again my lack of JSONs knowledge?
I selected template and it is fine.
Thank you again great site I will try to discover more possibilities.

#5

The type dropdown tells us how to evaluate and store the value.

  1. Payload path. This is a JSON path that points to somewhere on your payload. For for example if you wanted to store the first temperature, you could type body.body.body[1].temp in the textfield. We would then store the value on your payload at that path, which in your example is “20.7”.
  2. Template. This allows you to create a string that references values from the payload. So for example, if you wanted to store the string “The first temperature is 20.7”. You would set the value to The first temperature is {{ body.body.body[1].temp }}.
  3. JSON. This allows you to store an object by specifying it as JSON. This doesn’t allow you to pull any values from the payload. It is simply a way to store a hardcoded JSON object. For example { "foo" : "bar" }. It will say “Invalid JSON” until a valid JSON string is typed in the textfield.
  4. Empty. This simply clears out whatever is stored there.

The full documentation can be found here.

unlisted #6
listed #7