JSON export from emoncms

I'm trying to get an export of a feed from my emoncms instance in json format.

I'm using <server>/<path>/data.json?&apikey=<API>&id=<feedid>  and I'm getting back the right data but not in the format I expected.

The feed I'm currently working on is daily energy usage

What I had expected to get was a feed with the format:  

[{
   "Date" : <datevalue1>,
   "Energy" : <energyvalue1>,
  },
   {
  "Date" : <datevalue2>,
   "Energy" : <energyvalue2>,
  },
  ...
]

where what I'm getting is:

[[<datevalue1>,"<energyvalue1>"],[<datevalue2>,"<energyvalue2>"],...]  

where data is being stored in both the key (Date) and value (energy)

is it possible to export a json feed that only stores data in the values and where the keys only indicate what the data represents?

 

 

ukmoose's picture

Re: JSON export from emoncms

Given to impending changes to the structure of emoncms, I chose to write an intermediate php script to do the reformatting. I've attached it below, in case it helps out anyone else. 

<?php
$now = time() *1000;
$then = $now - (7*86400000);//I'm only interested in the last weeks worth of data
$str_data = file_get_contents("http://<server>/<path>/emoncms/feed/data.json?&apikey=<api>&start=".$then."&end=".$now."&id=42");
$tempmaxarray = json_decode($str_data,true);
$json = array();
foreach ($tempmaxarray as $v1) {
   $arr = array( 'date' => ($v1[0]), 'energy' => (floatval($v1[1])) );
   array_push($json, $arr);
   }
echo json_encode($json);
?>
PeterN's picture

Re: JSON export from emoncms

Hi, Sounds great. I'm monitoring my home with emoncms modular version on local server RPi . I would like to upload to central server daily or hourly and this sounds like a great way to do it.

Cheers

Peter

 

EnergyRnR's picture

Re: JSON export from emoncms

What's the correct path on emoncms.org? I've tried http://emoncms.org/emoncms3/feed/data.json?&apikey=APIKEY&id=1234 and other variantsbut to no avail....

Eamonn

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.