Page 1 of 1

Specific data from REST request array

Posted: Thu May 29, 2014 12:06 am
by Peter Jacobsen

I am able to pull my data correctly from Xively, it is from a thermostat and includes current temp, set temp, mode, etc. I have pasted the json response and it includes an array as pasted below.

How do I get my specific value from the "datastreams" array by specifing an id? I have tried writing some javascript and it always returns the first current_value. I am using the assignment as shown below. I have also included the json response to show the array structure:

"status":"live",
"updated":"2014-05-28T22:16:38.401214Z",
"created":"2014-05-19T01:50:18.782800Z",
"creator":"https://xively.com/users/pjacobs4",
"version":"1.0.0",
"datastreams":[
{
"id":"Mode",
"current_value":"2",
"at":"2014-05-28T22:16:38.233371Z",
"max_value":"3.0",
"min_value":"0.0"
},
{
"id":"mode_update",
"current_value":"0",
"at":"2014-05-27T22:45:07.877574Z",
"max_value":"0.0",
"min_value":"0.0"
},
{
"id":"Running",
"current_value":"1",
"at":"2014-05-28T22:16:38.233371Z",
"max_value":"7.0",
"min_value":"0.0"
},
{
"id":"Set_Temp",
"current_value":"70",
"at":"2014-05-28T22:16:38.233371Z",
"max_value":"85.0",
"min_value":"4.0",
"unit":{
"symbol":"F",
"label":"Degrees F"
}
},
{
"id":"Temperature",
"current_value":"72.6",
"at":"2014-05-28T22:16:38.233371Z",
"max_value":"83.5",
"min_value":"0.0",
"unit":{
"symbol":"F",
"label":"deg F"
}
},
{
"id":"temp_set_update",
"current_value":"0",
"at":"2014-05-27T22:44:46.975142Z",
"max_value":"0.0",
"min_value":"0.0"
}
],
"location":{
"name":"Home",
"domain":"physical",
"lat":42.5174949291418,
"lon":-83.089427947998
}, Image


Specific data from REST request array

Posted: Thu May 29, 2014 2:12 am
by Yurii Orishchuk

Hi Peter.

Please provide us JS code you currently using.

In accordance to your response you can use following code to get needed item by id:

precode

//data variable - is your response data you have mentioned before.

var datastreams = data.datastreams;

var neededId = "Set_Temp&quot

for(var i = 0; i < datastreams&#46;length; i++){
var datastream = datastreams;

Code: Select all

 if(datastream&#46;id == neededId){ 
     alert("Item with id = " + neededId + ", current_value = " + datastream&#46;current_value); 
     &#47;&#47;Here is code to use selected "datastream"&#46; 
 }; 

};

/code/pre

Regards.


Specific data from REST request array

Posted: Thu May 29, 2014 2:21 am
by Peter Jacobsen

I will give this a shot thanks for the help.


Specific data from REST request array

Posted: Thu May 29, 2014 3:06 am
by Peter Jacobsen

Worked great, I can now get all the variables in one call. I really appreciate the help!