i need some support in parsing a payload with javascript. Below you see the json body. I need to return only the valuetype "occupants".
{"id":"8684bc99-5a69-4b7a-92c2-69faa91b668b","type":"airthings-webhook-cloudevent-occupancy-ventilation-sample-feed","source":"https://dashboard.airthings.com/integrations/webhooks/2c823594-3c4b-4352-9929-8486a5f8a985","dataContentType":"application/json","labels":{},"data":[{"measurementSystem":"METRIC","serialNumber":"2930118961","recorded":"2022-05-04T08:55:00","occupants":4.0,"ventilationAmount":171.0,"ventilationState":true,"ratings":{},"sensorUnits":{"occupants":"occ","ventilationAmount":"m3h","relativeVentilationRate":"ach"}}],"time":"2022-05-04T08:58:20.322709","specVersion":"0.2"}
I made it to return the "data" array with the following decoder. Now i need the object "occupants" within data. Can someone tell me what I need to adjust in the following code?
function Decoder(request) {
// Parse JSON into Object
var payload = JSON.parse(request.body);
// Load Data from JSON
var data = payload.data;
var Occupancy = data.occupants;
//var humidity = data.hum;
//var battery = data.bat;
// Forward Data into Device using Serial
return data;
}