Demystifying Iron Ajax: Unraveling the process of parsing an array of JSON objects from a successful

When making an AJAX call to the server, I receive a response in the form of an array of objects as JSON.

[{"dms":[{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":37.8688,"longitude":-144.2093,"toolType":1},{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":31.8688,"longitude":-115.2093,"toolType":1}],"gyro":[{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":37.8688,"longitude":-144.2093,"toolType":1},{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":31.8688,"longitude":-115.2093,"toolType":1}]}]

After calling the success method and printing the data, I see [object, object] in the console. How can I parse this object of array of objects in the success method?

<!-- Import Polymer -->
<script src="../../bower_components/webcomponentsjs/webcomponentslite.js"></script>

<link rel="import" href="../../bower_components/polymer/polymer.html"/>
<link rel="import" href="../../bower_components/paper-item/paperitem.html">
<link rel="import" href="../../bower_components/paper-listbox/paperlistbox.html">
<link rel="import" href="../../bower_components/paper-dropdown-menu/paperdropdown-menu.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">

<!-- Seed app components -->
<dom-module id="tool-bar">
  <template>
    <iron-ajax
      id="ajax"
      url=" rest URL"
      params='{"type":"all"}'
      handle-as="json"
      content-type="application/json"
      method="GET"
      on-response="mapResponse"
      debounce-duration="3000">
    </iron-ajax>
  </template>
  <script>
    Polymer({
      is : 'tool-bar',
      properties: {
        gyrodata: {
          type: Array
        }
      },

      mapResponse: function (data) {
        console.log(data.detail.response); //[Object,Object]
      }
    });
  </script>
</dom-module>

Answer №1

After parsing the JSON data, I utilized three nested for loops in my success function:

mapResponse: function (data) {
  var response = data.detail.response;
  for(var i = 0;i < response.length; ++i) {
    for (var key in response[i]) {
      for (var j = 0; j < response[i][key].length; j++) {
        console.log(response[i][key][j].serialNo);
        console.log(response[i][key][j].status);
        console.log(response[i][key][j].firmwareStatus);
      }
    }
  }
}

If anyone knows of a simpler solution, please let me know.

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

The functionality of reordering columns, virtual scrolling, and resizing the grid in jqgrid are not functioning properly

Implementing jqgrid with Symfony to display a datagrid has been a challenging task for me. Thanks to Oleg's insightful response, many of the major issues have been resolved. Below is a snippet of my code: <link rel="stylesheet" type="text/css" ...

The most effective method in Django for transmitting a request from the template to the database for retrieving data

Currently, I am developing a Django project wherein I utilize Django views to extract data from a postgres table and showcase them in my template view. Within the template, I incorporate JQuery to enhance the functionality (like dropdown menus etc.). At p ...

Combining Two JSON Arrays Featuring Unique Keys

I have two JSON arrays with slightly different keys. The first JSON array contains the keys id, name, and title. The second JSON array includes id, title, forename, name, and company. I am wondering if it's possible to merge these two arrays so th ...

Sending an array of properties to a child component

I am attempting to pass an array of objects from a parent component to a child component as a prop, then iterate over it using map and display the items in the child component. However, when I try to use map on the array nothing is happening, even though ...

Mastering the Art of Resizing Video Controls: A

https://i.stack.imgur.com/jTgap.png https://i.stack.imgur.com/Exf6Y.png On the initial screenshot, the video player displays normal controls. However, on the same website with identical CSS, the second video player shows different control settings. // C ...

Remove an element from an array within an object stored in a MongoDB document

Greetings and thank you for taking the time to read my query! I am currently diving into the world of coding, and here's my first question on this platform: I have a MongoDB collection structured similarly to the example below. Each document represe ...

PolymerCLI encountered an issue: Please execute the command "npm install -g polymer-cli-1.5.7.tgz" to resolve it

Here are the steps to follow: Begin by installing polymer-cli Use this command: npm install polymer-cli Next, install the bundled-deps : Use this command: npm install -g bundle-deps After that, run this command : bundle-deps Lastly, run this command : ...

Uploading photos to Postgres through express/multer

Currently, I am using Postman to send images to a POST route through the Express library. However, when I retrieve the buffer of binary data, I am unable to process it accordingly. Will using body-parser resolve this issue? This is how I am uploading the ...

Ways to clear the GET parameters in a MySQL/jQuery ajax request

When performing an ajax call to fetch data from a MySQL database and setting it to run at regular intervals, the question arises about where to store the GET values for clearing purposes. Below is a snippet of code in test.js: var fill = function () { ...

Pass more information along with form_data using ajax

Firstly, I would like to showcase a demo illustrating the behavior that I am aiming to achieve. $('#multiple_files').change(function(){ var form_data = new FormData(); for(var i=0; i<files.length; i++) { form ...

Issue with Ajax request in Laravel failing to function when ad blocker is active

While utilizing Laravel to send Ajax requests, everything functions properly on localhost. However, once I test it on the live server with an adblocker enabled, it fails to work. Once the adblocker is disabled, the functionality returns. domain.com/sponso ...

What is the best way to upgrade to a specific version of a child dependency within a module?

npm version: 7.24.2 Looking for assistance on updating a child dependency. The dependency in question is: vue-tel-input This dependency relies on libphonenumber-js with version ^1.9.6 I am aiming to update libphonenumber-js to version ^1.10.12. I have ...

MongoDB table collections (table names in other databases)

After setting up my express server to connect to mongodb, I encountered an issue despite everything working fine initially. I created a collection in my mongodb called projects (plural form). In my project.model.js file, I defined the model as follows: c ...

When using JSON.Stringify in a React App, it only displays the first item and the length of the object instead of all the other items

Working on a React App, I encountered an issue while trying to convert an array to JSON and send it to the server. My approach was like this: console.log(JSON.stringify(mainArray)) I anticipated seeing output like this during testing: "breakfast": { ...

Monitor a user's activity and restrict the use of multiple windows or tabs using a combination of PHP and

I am looking to create a system that restricts visitors to view only one webpage at a time, allowing only one browser window or tab to be open. To achieve this, I have utilized a session variable named "is_viewing". When set to true, access to additional ...

NPM is having trouble locating a shell script

An error is encountered when running npm run build: '.' is not recognized as an internal or external command, operable program or batch file. Here is the npm file that contains relevant scripts: "scripts": { "postinstall": "jspm instal ...

Implement the color codes specified in the AngularJS file into the SASS file

Once the user logs in, I retrieve a color code that is stored in localstorage and use it throughout the application. However, the challenge lies in replacing this color code in the SASS file. $Primary-color:#491e6a; $Secondary-color:#e5673a; $button-color ...

Link together a series of AJAX requests with intervals and share data between them

I am currently developing a method to execute a series of 3 ajax calls for each variable in an array of data with a delay between each call. After referring to this response, I am attempting to modify the code to achieve the following: Introduce a del ...

Resolving the "Error: Cannot update a React state on an unmounted component" issue

I encountered a console error while attempting to navigate to a new page within my web application. Here's the error message I received: Warning: A React state update was attempted on an unmounted component, which is essentially a no-op. However, t ...

How to implement mouse hover functionality in C# using Selenium?

When attempting to mouse hover on a menu with multiple sub-menus, I encountered an issue where the suggested actions caused other menus to overlap and hide the intended element. Below is the recommended code snippet for hovering over the desired element: ...