Retrieving a time series data set from a JSON stream

Even though ThingSpeak offers great charts, I'm interested in retrieving data from ThingSpeak and creating my own visualizations using Google Charts. When extracting a "feed" from ThingSpeak, the data is presented in a JSON object like the one below:

{
  "channel":
  {
     "id": 9,
     "name": "my_house",
     "description": "Netduino Plus connected to sensors around the house",
     "latitude": "40.44",
     "longitude": "-79.996",
     "field1": "Light",
     "field2": "Outside Temperature",
     "created_at": "2010-12-13T20:20:06-05:00",
     "updated_at": "2014-02-26T12:43:04-05:00",
     "last_entry_id": 6060625
  },
  "feeds":
       [
          {
            "created_at": "2014-02-26T12:42:49-05:00",
            "entry_id": 6060624,
            "field1": "188",
            "field2": "25.902335456475583"
          },
          {
            "created_at": "2014-02-26T12:43:04-05:00",
            "entry_id": 6060625,
            "field1": "164",
            "field2": "25.222929936305732"
          }
      ]
  }

I've been trying to find a way to extract the "created_at" and "field1" (and maybe "field2") data into a table or array format, but I haven't found a solution yet. This is the kind of output I want to achieve:

[
['created_at',                'field1', 'field2'],
['2014-02-26T12:42:49-05:00', 188,      25.902335456475583],
['2014-02-26T12:43:04-05:00', 164,      25.222929936305732]
]

Any suggestions on how I can accomplish this task?

Answer №1

My approach to solving this problem is as follows. (Please note that I referred to code from the following source to understand how to access object properties dynamically: How to iterate through property names of Javascript object?)

var data = {
  "channel":
  {
     "id": 9,
     "name": "my_house",
     "description": "Netduino Plus connected to sensors around the house",
     "latitude": "40.44",
     "longitude": "-79.996",
     "field1": "Light",
     "field2": "Outside Temperature",
     "created_at": "2010-12-13T20:20:06-05:00",
     "updated_at": "2014-02-26T12:43:04-05:00",
     "last_entry_id": 6060625
  },
  "feeds":
       [
          {
            "created_at": "2014-02-26T12:42:49-05:00",
            "entry_id": 6060624,
            "field1": "188",
            "field2": "25.902335456475583"
          },
          {
            "created_at": "2014-02-26T12:43:04-05:00",
            "entry_id": 6060625,
            "field1": "164",
            "field2": "25.222929936305732"
          }
      ]
  }

var results = [];
var feeds = data.feeds;

var headers = [];
for (var key in feeds[0]) {
    headers.push(key);
}
results.push(headers);

for (var x = 0; x < feeds.length; x++) {
    var row = [];
    var feed = feeds[x];
    for (var y = 0; y < headers.length; y++) {
        var header = headers[y];
        row.push(feed[header]);
    }
    results.push(row);
}

console.log(results);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Bloodhound with a JSON generated using Flask's jsonify function is a seamless process

Recently, I have been exploring the Bloodhound typeahead feature to implement database search functionality in my Flask application. I found a helpful guide on how to set this up at: Twiiter Typeahead Custom Templates - Getting Default Example Working $(d ...

Utilizing a Meteor Method within a Promise Handler [Halting without Throwing an Error]

I've been working on integrating the Gumroad-API NPM package into my Meteor app, but I've run into some server-side issues. Specifically, when attempting to make a Meteor method call or insert data into a collection within a Promise callback. Be ...

Implementing Asynchronous Custom Validators in Angular 4

I've encountered the following code snippet: HTML: <div [class]="new_workflow_row_class" id="new_workflow_row"> <div class="col-sm-6"> <label class="checkmark-container" i18n>New Workflow <input type="che ...

Navigate to the appropriate Angular route using HTML5 mode in Rails

After removing the '#' symbol in angular by using html5Mode, everything seemed to work fine. However, upon refreshing the page, it started looking for the template in Rails instead of Angular, resulting in a "template not found" error. Angular R ...

Configure environment variables for either grunt or grunt-exec

Attempting to utilize grunt-exec for executing a javascript test runner while passing in a deployed link variable. This is being done by initializing an environment variable grunt.option('link') with the help of exec:setLink. Within my test_runn ...

I am facing an issue with my useFetch hook causing excessive re-renders

I'm currently working on abstracting my fetch function into a custom hook for my Expo React Native application. The goal is to enable the fetch function to handle POST requests. Initially, I attempted to utilize and modify the useHook() effect availab ...

The API request for /api/auth/callback/credentials was resolved successfully, but no response was sent back. This could potentially lead to delays

When attempting to log in with Talend API Tester, I encountered the following message in the terminal: API resolved without sending a response for /api/auth/callback/credentials, this may result in stalled requests. Additionally, here is the screenshot ...

Mapping a bar chart on a global scale

Are there any methods available to create bar charts on a world map? The world map could be depicted in a 3D view resembling a Globe or in a 2D format. It should also have the capability to zoom in at street level. Does anyone have suggestions or examples ...

Display only the labels of percentages that exceed 5% on the pie chart using Chart JS

I'm currently diving into web development along with learning Chart.js and pie charts. In my project, the pie chart I've created displays smaller percentage values that are hardly visible, resulting in a poor user experience on our website. How c ...

When sending a JSON string in an HTTP request with Express, the req.body variable may be empty

I'm facing an issue where Express is receiving an empty JSON string {} and I've been struggling to identify the cause. I've attempted using both bodyParser and express.json for the JSON parser, but the result remains the same. I've also ...

Rapidly update code changes using the development mode in Next.js with the VS Code Remote Container/devcontainer

Struggling to enable Next.js' Fast Refresh feature while using a VS Code Remote Container. Running npm run dev displays the app on localhost, indicating the container functions properly - but Fast Refresh remains ineffective. Next.js version: v11.0.1 ...

Creating clickable data columns in jQuery DataTablesWould you like to turn a column in your

How can I turn a column into a clickable hyperlink in a jQuery DataTable? Below is an example of my table structure: <thead> <tr> <th>Region</th> <th>City</th> <th> ...

Preventing autoscrolling in Ionic's dual side menu template when additional content is added

Could anyone kindly assist me in figuring out why the autoscrolling of the content is not functioning correctly? Whenever the button on the header is clicked, a new message will be included in the main content. However, once the number of lines exceeds wha ...

Create an Angular directive that highlights a div if any of its child inputs have focus

I've developed an Angular directive for a repetitive section containing form elements. My aim is to have the entire section highlighted whenever any input field inside it is focused. template.html <div class="col-md-12 employee-section"> <l ...

Is it more advantageous to create two distinct projects for the frontend and backend along with an API, or should I consolidate them into a

Asking for some guidance on a few queries I have. I've developed a backend node server with express & mongo to run specific tasks and store them in the database, along with creating an admin page using express & bootstrap. Everything is functioning s ...

Loop through the .getJSON response using jQuery

I've searched high and low for an answer to my question, but I can't seem to find one. My issue is with iterating over a JSON array (and its object) using jQuery's .each() method to print out all the values. Here is the JSON structure: { ...

Turn off sticky sidebar feature for mobile-friendly layout

I have encountered an issue with my script that I need assistance with. I am trying to disable this script for responsive or mobile view, but despite trying various methods, it is not functioning as expected. <script type="text/javascript"> $(func ...

Is it possible to make API calls within the componentWillMount lifecycle method in React?

I've been immersed in the world of React for the past year. The standard practice within my team is to make an API call in componentDidMount, fetch the data, and then setState after receiving the data. This ensures that the component has fully mounted ...

Conceal a secret input element's value upon clicking a submit button (using PHP and jQuery)

Greetings, I'm facing an issue and need your assistance: Here's the scenario - I have a form that gathers First Name, Last Name, and City from the user. Upon clicking submit, the provided information is then showcased within a table as follows: ...

Unexpected JSON response generated by my PHP script following an AJAX request

I'm still learning and I seem to be making a mistake somewhere. I have a PHP result set that I need to iterate through, like this: $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows[] = $r; } echo json_encode ...